[英]How can I print out the int values from the array?
The code below shows my progress, but I cannot print the numbers that were entered. 下面的代码显示了我的进度,但是我无法打印输入的数字。 I don't know where to put
println("you entered the following numbers")
in the loop so that it'll show up when the loop stops. 我不知道将
println("you entered the following numbers")
放在循环中的什么位置,以便在循环停止时显示它。
import java.util.Scanner;
public class aufgabe5 {
public static void main(String[] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("How much numbers do you want to enter?");
x = input.nextInt();
int j = 1;
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[x];
for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter the " + j++ + ". number:");
numbers[i] = scanner.nextInt();
}
System.out.println("You entered following numbers");
System.out.println(x);
}
}
Change x
like 改变
x
喜欢
System.out.println(x);
to Arrays.toString(int[])
like 像
Arrays.toString(int[])
System.out.println(Arrays.toString(numbers));
Edit 编辑
To print the array reversed, 要反转打印阵列,
String str = Arrays.toString(numbers).replace(", ", " ,");
str = str.substring(1, str.length() - 1);
System.out.println(new StringBuilder(str).reverse().insert(0, "[")
.append("]"));
Intialize the String before the loop, then keep adding the values to it. 在循环之前初始化String,然后继续向其中添加值。 After the loop finishes you can print the whole thing.
循环完成后,您可以打印整个内容。
String someVar="You entered following numbers: ";
for(int 1=0;i<array.length;i++)
{
someVar=someVar+numbers[i]+",";
}
System.out.println(someVar);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.