Java 使用 For 循环填充数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22239532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a For loop to populate an array
提问by Bhetzie
So I've been trying to get an array of values from a scanner and fill them in array. If the user enters 10 integer values or -1 the loop ends and the array stops filling in values. The following is the code I have written so far to do this:
所以我一直在尝试从扫描仪获取一组值并将它们填充到数组中。如果用户输入 10 个整数值或 -1,则循环结束并且数组停止填充值。以下是我到目前为止编写的代码:
// Input scanner and input controls
Scanner scan = new Scanner(System.in);
System.out.print("Please enter 10 integers \n");
int maxnum = 10;
boolean done = false;
int count = 0;
int[] arrays = new int[maxnum];
// Store values in count array
for (count = 0; (count < maxnum && !done); count++) {
arrays[count] = scan.nextInt();
if (arrays[count] == -1) {
arrays[count] = 0;
done = true;
}
}
// print count values
for (count = 0; (count < maxnum); count++) {
if (arrays[count] != 0) {
System.out.println(arrays[count]);
}
}
What I have done so that -1 is not stored in the array is set the value of -1 to 0 and then tell my next loop not to print any values of 0. I know there has to be a better way to do this and I understand that I am still storing the zero values, but for the life of me I can not figure out how to prevent the loop from storing zero values in the array
我所做的使 -1 不存储在数组中的操作是将 -1 的值设置为 0,然后告诉我的下一个循环不要打印任何 0 值。我知道必须有更好的方法来做到这一点,并且我知道我仍在存储零值,但在我的一生中,我无法弄清楚如何防止循环在数组中存储零值
采纳答案by Mc Kevin
how about storing the input to another variable and checking before populating to the array?
如何将输入存储到另一个变量并在填充到数组之前进行检查?
for (count = 0; (count < maxnum && !done); count++) {
int testnum = scan.nextInt();
if (testnum == -1) {
done = true;
}
else {
arrays[count] = testnum;
}
}
回答by dasblinkenlight
Since the number of entries may be less than maxnum, a proper way of dealing with the problem would be to store the actual number of numbers that the user has entered, not counting the -1, and then stop the secondloop before the last number is printed. This way you wouldn't care if -1is in the array or not, because it wouldn't get printed anyway.
由于条目数可能少于maxnum,处理该问题的正确方法是存储用户输入的实际数字数,不计算-1,然后在打印最后一个数字之前停止第二次循环。这样你就不会关心是否-1在数组中,因为它无论如何都不会被打印出来。
You already keep the count, all you need to do is using it in your second loop:
您已经保留了count,您需要做的就是在第二个循环中使用它:
for (int i = 0; i < count ; i++) {
System.out.println(arrays[i]);
}
回答by Arbiter
Primitive data types (including their arrays) are always assigned to their default value if another assignment hasn't happened before referencing. In the case of int, it's default value is 0 (same with byte, short, char, long), and 0.0 for floatand double. Object instance's default value is null. Because you are using an int[]array, all of the objects within it are automatically assigned to 0 if you haven't done so already.
如果在引用之前没有发生其他赋值,则原始数据类型(包括它们的数组)总是被赋值为它们的默认值。在的情况下int,它的默认值是0(同与byte,short,char,long),和0.0float和double。对象实例的默认值为null。因为您使用的是一个int[]数组,所以如果您还没有这样做,其中的所有对象都会自动分配为 0。
But since you do not want to store a 0 in the array, then don't. Use a different number, -1 is fine for that, unless you have any other preferences.
但既然你不想在数组中存储 0,那就不要。使用不同的数字,-1 就可以了,除非您有任何其他偏好。
回答by aliteralmind
This stores only numbers that are not -1. When -1 is entered, the loop is terminated.
这仅存储非 -1 的数字。当输入 -1 时,循环终止。
import java.util.Scanner;
/**
<P>{@code java TenIntsOrNeg1}</P>
**/
public class TenIntsOrNeg1 {
public static final void main(String[] ignored) {
int maxInts = 10;
int[] ints = new int[maxInts];
Scanner scan = new Scanner(System.in);
System.out.println("Please enter 10 integers, or -1 to quit.");
int idx = 0;
//Keep going for all ten elements...
while(idx < 10) {
System.out.print((idx + 1) + ": ");
int inputNum = scan.nextInt(); //Assumes it *IS* an int
//...unless they enter -1 to terminate.
if(inputNum == -1) {
//End-condition. Number is not stored
break;
}
//Not -1. Store it.
ints[idx++] = inputNum;
}
System.out.println("DONE. Printing:");
for(int i = 0; i < ints.length; i++) {
System.out.println(i + ": " + ints[i]);
}
}
}
Output:
输出:
[C:\java_code\]java TenIntsOrNeg1
Please enter 10 integers, or -1 to quit.
1: 6
2: 1
3: 2
4: 8
5: 121
6: -1
DONE. Printing:
0: 6
1: 1
2: 2
3: 8
4: 121
5: 0
6: 0
7: 0
8: 0
9: 0

