Java 如何根据用户输入打印多个整数数组?
我正在创建一个java程序,它提示用户输入数组中的项数(一个非负整数),读取它,并将它保存在一个int变量中。然后,它会提示用户输入所有项目的值,并将它们保存在int数组中。然后,程序以[x1,x2,…,xn]的形式打印数组的内容。不需要用户验证。程序本来应该运行得很好,但是,当我尝试为用户输入多个输入时,输出有问题 而不是显示这一点:Java 如何根据用户输入打印多个整数数组?,java,Java,我正在创建一个java程序,它提示用户输入数组中的项数(一个非负整数),读取它,并将它保存在一个int变量中。然后,它会提示用户输入所有项目的值,并将它们保存在int数组中。然后,程序以[x1,x2,…,xn]的形式打印数组的内容。不需要用户验证。程序本来应该运行得很好,但是,当我尝试为用户输入多个输入时,输出有问题 而不是显示这一点: Enter the number of items: 2 Enter the value of all items (separated by space):
Enter the number of items: 2
Enter the value of all items (separated by space): 88
Enter the value of all items (separated by space): 99
The values are: [88, 99]
输出如下所示:
Enter the number of items: 2
Enter the value of all items (separated by space) : 88
Enter the value of all items (separated by space) : 99
The values are: [88]
The values are: [99]
另外,当我为项目数输入0时,输出应该显示这一点
Enter the number of items: 0
The values are: []
但我的输出显示了以下内容:
Enter the number of items: 0
当项目数为0时,它甚至不显示值的括号。以下是我的编码:
import java.util.Scanner;
public class PrintArray{
public static void main(String[] args) {
// Declare variables
int numItems;
int[] items; // Declare array name, to be allocated after numItems is known
// Prompt for a non-negative integer for the number of items;
// and read the input as "int". No input validation.
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of items: ");
numItems = in.nextInt();
// Allocate the array
items = new int[numItems];
// Prompt and read the items into the "int" array, if array length > 0
if (items.length > 0) {
for (int i = 0; i < items.length; ++i) {
System.out.print("Enter the value of all items (separated by space) : ");
items[i] = in.nextInt();
}
}
// Print array contents, need to handle first item and subsequent items differently
for (int i = 0; i < items.length; ++i) {
if (i == 0) {
// Print the first item without a leading commas
System.out.println("The values are: [" + items[i] + "]");
} else {
// Print the subsequent items with a leading commas
System.out.println("The values are: [" + items[i] + "]");
}
}
}
}
import java.util.Scanner;
公共类打印数组{
公共静态void main(字符串[]args){
//声明变量
int numItems;
int[]items;//声明数组名,在numItems已知后分配
//提示输入项目数的非负整数;
//并将输入读取为“int”。无输入验证。
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入项目数:”);
numItems=in.nextInt();
//分配数组
项目=新整数[numItems];
//如果数组长度>0,则提示并将项目读入“int”数组
如果(items.length>0){
对于(int i=0;i
如果有人能帮我,那就太好了。非常感谢 您正在对同一数组进行迭代,因此它会被打印多次。请在最后一次的
中尝试以下操作:
String values = ""
for (int i = 0; i < items.length; ++i) {
if (i == 0) {
// Print the first item without a leading commas
values = values + items[i];
} else {
// Print the subsequent items with a leading commas
values = values + "," + items[i];
}
System.out.println("The values are: [" + values + "]");
String value=“”
对于(int i=0;i
或者您也可以这样做,这样看起来更具可读性:
String values = "" + items[0];
for (int i = 1; i < items.length; ++i) {
// Print the subsequent items with a leading commas
values = values + "," + items[i];
}
System.out.println("The values are: [" + values + "]");
String value=”“+项[0];
对于(int i=1;i
在循环中,您正在打印整个句子,您可以将数组元素附加到字符串中,以便稍后打印。
第二个问题是因为数组是空的,这使得循环条件在第一次迭代本身中为假。因此循环体不会被执行
你可以试试下面的方法
String output = "[";
for (int i = 0; i < items.length; ++i) {
output += items[i] + ", ";
}
// remove last comma and space
output = output.substring(0, output.length() - 2);
output += "]";
System.out.println("The values are: " + output);
字符串输出=“[”;
对于(int i=0;i
只需将System.out.println(“值为:[”);
移动到循环之前,将System.out.println(“]);
移动到循环之后。并且只打印项[i]
在您的循环中。编辑:您可能想将其更改为System.out.print
以使其在同一行中谢谢您提供的信息!我的代码运行良好:)您好,谢谢您提供的信息,我的代码运行良好:)。但是,我想知道如何在输出之间添加间距。例如,代替输出显示Ing[3,2,4,5,6],我希望我的输出是[3,2,4,5,6],只需在coma后面添加空格,如下所示:values=values+”,“+items[I];