[英]How do I reference a user-entered int?
Easy question (I'm a beginner!)... My java program needs to refer back to the user-entered integers stored as variables "a" and "z". 一个简单的问题(我是初学者!)...我的Java程序需要引用存储为变量“ a”和“ z”的用户输入的整数。 These two variables are initialized using a Scanner and the nextInt() method.
这两个变量是使用Scanner和nextInt()方法初始化的。 Here's my code:
这是我的代码:
Scanner in = new Scanner(System.in);
int a;
int z;
int x;
System.out.println("Please provide a lower bound (integer): ");
a = in.nextInt();
System.out.println("Please provide an upper bound (integer): ");
z = in.nextInt();
So now I need to refer back to these user-entered variables in a for-loop that appears later on, but I do not know the syntax to do this. 因此,现在我需要在稍后出现的for循环中引用这些用户输入的变量,但是我不知道执行此操作的语法。 I need the for-loop to start at "a" and continue one-by-one until "z".
我需要for循环才能从“ a”开始,然后一个接一个地继续直到“ z”。
for(a ; a <= z ; a++) {
//code irrelevant to my question
}
Java is making it very clear that I cannot just write the variables in the for-loop, so how do I do it? Java非常清楚地表明,我不能只在for循环中编写变量,那么该怎么办呢? Thanks!
谢谢!
You want to do that with a loop counter, let it be i
: 你想用一个循环计数器做到这一点,让它成为
i
:
for (int i = a; i <= z; i++) {
//process i
}
In this way you have i
that starts at a
and increases up to z
. 这样,您的
i
从a
开始并增加到z
。
To start at a and end on z, inclusive: 要从a处开始并在z处结束,包括:
for(; a <= z ; a++) {
To stop right before z: 要在z之前停止:
for(; a < z ; a++) {
This will change the variable a
. 这将更改变量
a
。
You don't need to fill that space before the first semi-colon. 您无需在第一个分号之前填充该空间。 That's the "initialize counter" spot, and you've already initialized your counter from user input above (before) the loop.
那就是“初始化计数器”的位置,并且您已经从循环上方(之前)的用户输入初始化了计数器。
If you don't want a
to change, then simply duplicate its value into a temporary one, as in @skiwi's answer: 如果你不想
a
改,然后简单地复制它的值写入暂时的,如@ skiwi的回答:
for(int i = a; i <= z; i++) {
Here's some good information: https://www.google.com/search?q=for+loop+java 这里有一些很好的信息: https : //www.google.com/search?q=for+loop+java
If you want to include z: 如果要包括z:
for(int i=a;i<=z;i++){
[stuff]
}
or, if you want it to exclude z: 或者,如果您希望它排除z:
for(int i=a;i<z;i++)
[stuff]
}
This is because the format for for loops is for(int [counter]=[start value];[counter][condition][limit];[counter][change amount])
. 这是因为for循环的格式为for
for(int [counter]=[start value];[counter][condition][limit];[counter][change amount])
。 For example, if you wanted something to loop up to (including) two, including zero, your code would be for(int i=0; i<3;i++)
, while if you wanted it to loop down from two to zero it would be for(int i=2; i>=0;i--)
. 例如,如果您希望某个东西循环到(包括)两个(包括零),那么您的代码将为
for(int i=0; i<3;i++)
,而如果您希望它从两个循环到零,则为将会是for(int i=2; i>=0;i--)
。 To make the loop skip values just change the amount the counter increases/decreases by, such as i+=2
, or i-=2
要使循环跳过值,只需更改计数器增加/减少的量即可,例如
i+=2
或i-=2
Existing answers tell you how to fix it, but don't explain what the problem is. 现有的答案告诉您如何解决它,但不解释问题所在。 If you try to compile the class, you'll see the following error
如果您尝试编译该类,则会看到以下错误
$ javac SomeClass.java
SomeClass.java:16: error: not a statement
for(a ; a <= z ; a++) {
^
That's because a
by itself is not a statement. 那是因为
a
本身并不是一个陈述。
The simplest thing is to just omit the a
since you don't need to initialize it 最简单的事情是省略
a
因为您不需要初始化它
for(; a <= z ; a++) {}
Alternatively, you could create a looping variable so you don't affect a
and you can initialize it in the loop so it looks more like a normal loop. 另外,您可以创建一个循环变量,这样就不会影响
a
并且可以在循环中对其进行初始化,因此它看起来更像是普通循环。
for(int i = a; i <= z ; i++) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.