2015-07-19 69 views
0

我试图得到一堆数字作为输入,并输出它作为第一个未排序的数组,然后排序。如何将输入值存储在for循环内的数组中?现在,它只存储最后输入的值。如何将值存储在for循环中的数组中?

String strNumberOfValues, strValue; 
int intNumberOfValues, intValue; 
Scanner input = new Scanner(System. in); 
System.out.println("Please enter the number of values you would like to enter"); 
strNumberOfValues = input.nextLine(); 
intNumberOfValues = Integer.parseInt(strNumberOfValues); 
for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intValue = Integer.parseInt(strValue); 
    for (int j = 0; j == 0; j++) { 
     int[] intArray = { 
      intValue 
     }; 
     System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 

回答

4

声明并在循环开始前,初始化数组:

intNumberOfValues = Integer.parseInt(strNumberOfValues); 
int[] intArray = new int[intNumOfValues]; 
for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intArray[i] = Integer.parseInt(strValue); 
} 
System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 
. . . 

请注意,你的内循环是没用的。它只执行一次,循环索引j不用于循环。它所做的只是更多地限制intArray的声明范围,因此即使打印数组值的行也未定义该符号。顺便说一下,该打印语句不应该执行,直到所有输入值已被获取,这就是为什么我将它移到我的答案中的外环之后。 (还请注意,变量intValue不再需要在这里,可以从程序中删除,除非在别处使用。)

作为一种风格,我还建议您避免使用变量类型作为前缀为你的变量名称。

0

您必须首先声明数组,然后根据索引分配值。

String strNumberOfValues, strValue; 
int intNumberOfValues, intValue; 

Scanner input = new Scanner(System. in); 
System.out.println("Please enter the number of values you would like to enter"); 

strNumberOfValues = input.nextLine(); 
intNumberOfValues = Integer.parseInt(strNumberOfValues); 

int [] intArray = new int[intNumberOfValues]; 

for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intValue = Integer.parseInt(strValue); 
    intArray[i] = intValue; 
} 
    System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 
+0

究竟为什么你重新职位,给予近三分钟前一个答案? – Turing85

+0

:-)我在5分钟之前开始回答这个问题。但格式化需要更多时间。 – ganeshvjy

0

在您的inner for循环中,您每次都重新声明数组,所以只有最后一个值才会被保存。你必须与大小事先声明数组,然后输入用户在一个for循环填充它指数指数:

String strNumberOfValues, strValue; 
int intNumberOfValues, intValue; 
Scanner input = new Scanner(System. in); 
System.out.println("Please enter the number of values you would like to enter"); 
strNumberOfValues = input.nextLine(); 
intNumberOfValues = Integer.parseInt(strNumberOfValues); 
int[] intArray = new int[intNumberOfValues]; 

for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intValue = Integer.parseInt(strValue); 
    intArray[i] = intValue; 
} 
System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 
+0

请不要将数组称为“列表”。他们根本不同。 – Turing85

+0

@ Turing85我刚刚在原始问题中复制了该打印语句。输出可能不适用于技术用户。我认为单词“list”被用作一个正常的单词,而不是对编程结构的引用。 – Zarwan

+0

这是不正确的。一个'List'是一个动态数据结构。数组不是。任何使用某种命令式编程语言的人都应该知道差别(例如OP)。如果你不想编写“数组”(对于非技术性的最终用户),你可以通过写一些类似''的东西来避免这种情况。“这里是你输入的元素:''。 – Turing85

相关问题