gpt4 book ai didi

java - 如何在数组中打印 [ ] (Java)

转载 作者:行者123 更新时间:2023-12-01 17:47:38 24 4
gpt4 key购买 nike

我想从用户那里获取 10 个数字并将它们输入到一个新数组中。如何将 [ ] 放入我的输出中?

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num = 10;
List<Integer> inputList = new ArrayList<>(num);
while (num-- > 0) {
inputList.add(scnr.nextInt());
}
List<Integer> goofyArray = new ArrayList<>(inputList.size());
for (int i = inputList.size() - 1; i >= 0; i--) {
if(inputList.get(i) % 7 == 0){continue;}
if(inputList.get(i) < 0){
goofyArray.add((inputList.get(i) * -1) * 2);
} else {
goofyArray.add(inputList.get(i));
}
}
for (int number : goofyArray) {
System.out.print( number + " ");
}
}
}

最佳答案

How can I put [ ] into my output?

使用 util 包中的 Arrays.toString(int array[])

System.out.println(Arrays.toString(goofyArray));

关于java - 如何在数组中打印 [ ] (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53305517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com