Java 打印出所有输入
我只想让这个程序打印输出第二行到最后一行的所有输入。请帮我修一下Java 打印出所有输入,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,我只想让这个程序打印输出第二行到最后一行的所有输入。请帮我修一下 public static void main(String[] args) { int[] array = new int[10]; int sum = 0; int i = 0; Scanner input = new Scanner(System.in); while (sum <= 5 && i < array.l
public static void main(String[] args) {
int[] array = new int[10];
int sum = 0;
int i = 0;
Scanner input = new Scanner(System.in);
while (sum <= 5 && i < array.length) {
System.out.print("Enter the " + (i + 1) + "th number: ");
array[i] = input.nextInt();
sum += array[i];
i++;
}
System.out.print("Your digits are: ");
System.out.print(array[i] + " ");
System.out.println("\nsum is " + sum);
}
}
publicstaticvoidmain(字符串[]args){
int[]数组=新的int[10];
整数和=0;
int i=0;
扫描仪输入=新扫描仪(System.in);
而(sum只打印元素i处的数组一次,因为它不在循环中
如果要打印出数组中的每个元素,可以使用这样一个增强的for循环
for(int x : array){
System.out.print(x + " ");
}
int x = 0;
for(; x < i; x++){
System.out.print(array[x] + " ");
}
这应该循环遍历数组中的每个元素,将值赋给x并将其打印出来
或
可以创建一个循环,只打印在数组中输入的数字,如下所示
for(int x : array){
System.out.print(x + " ");
}
int x = 0;
for(; x < i; x++){
System.out.print(array[x] + " ");
}
intx=0;
对于(;x
希望这是您想要的。希望它有帮助。您只在元素i处打印数组一次,因为它不在循环中
如果要打印出数组中的每个元素,可以使用这样一个增强的for循环
for(int x : array){
System.out.print(x + " ");
}
int x = 0;
for(; x < i; x++){
System.out.print(array[x] + " ");
}
这应该循环遍历数组中的每个元素,将值赋给x并将其打印出来
或
可以创建一个循环,只打印在数组中输入的数字,如下所示
for(int x : array){
System.out.print(x + " ");
}
int x = 0;
for(; x < i; x++){
System.out.print(array[x] + " ");
}
intx=0;
对于(;x
希望这是您想要的。希望能有所帮助。非常感谢先生)非常感谢先生