英文:
I want to loop while in Java until user inputs the Right value but I am not able to do
问题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a, b;
b = 0;
System.out.print("Write something here: ");
a = scan.nextInt();
// 我希望用户只输入1或2,代码应该循环,直到用户输入为1或2
while (a != 1 && a != 2) {
b++;
System.out.println("Hello " + b);
a = scan.nextInt();
}
}
英文:
I want user to input the right value and loop the request until user does the same. But This code is not working, Can anyone correct me? Please help.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a, b;
b= 0;
System.out.print("Write something here: ");
a = scan.nextInt();
// I want user to input 1 or 2 only, The code should Loop until User inputs 1 or 2
for (a=0; a<1 && a>2;){
b++;
System.out.println("Hello "+ b);
a = scan.nextInt();
}
}
答案1
得分: 3
在你的 for 循环条件中,你正在检查 a 是否小于 1 且大于 2。这显然是不可能的情况,因此 for 循环永远不会执行。如果你坚持要使用 for 循环(虽然也许使用 while 循环会更好),请将条件改为:
a < 1 || a > 2
我更倾向于使用 while 而不是 for:
while (a != 1 && a != 2)
英文:
In your for conditional, you're checking whether a is smaller than 1 AND larger than 2. That's obviously never the case, so the for loop is never executed. If you insist on using a for loop (though perhaps a while loop would be better), change the conditional to be:
a < 1 || a > 2
My preference would be to use while instead of for:
while (a != 1 && a != 2)
答案2
得分: 1
一个数字可以同时小于 `1` 且大于 `2` 吗?不可以,对吧?
然后,检查你的条件,它说,`a<1 && a>2`,现在你知道其中的问题了。
我在你的程序中看到另一个问题是
a = scan.nextInt();
for (a=0;...)
如果你将 `a` 重置为 `0`,那么从用户那里获取输入有什么用呢?
你想要做的是:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a;
do {
System.out.print("在这里写点什么:");
a = scan.nextInt();
} while (!(a == 1 || a == 2));
// 显示正确的输入
System.out.println("最终,你已经输入了正确的值,即 " + a);
}
}
**一个示例运行:**
在这里写点什么:3
在这里写点什么:-7
在这里写点什么:4
在这里写点什么:0
在这里写点什么:2
最终,你已经输入了正确的值,即 2
英文:
Can a number be less than 1
and greater than 2
at the same time? No, right?
Then, check your condition, which says, a<1 && a>2
and now you know what is wrong with it.
Another problem that I see in your program is
a = scan.nextInt();
for (a=0;...)
What's the use of taking input from the user if you are resetting a
to 0
?
What you want to do is:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a;
do {
System.out.print("Write something here: ");
a = scan.nextInt();
} while (!(a == 1 || a == 2));
// Display the correct input
System.out.println("Finally, you have entered the correct value, which is " + a);
}
}
A sample run:
Write something here: 3
Write something here: -7
Write something here: 4
Write something here: 0
Write something here: 2
Finally, you have entered the correct value, which is 2
答案3
得分: 0
对于这种类型的循环,我建议使用“while”循环,而不是“for”循环(你说你想使用while循环,但实际上使用了for循环)。
“for”循环用于您事先知道要循环运行的次数的情况。
“while”循环用于您不知道循环将运行多少次的情况 - 在您的情况下取决于用户输入。
另外,在您的“for”循环中,您设置了条件,即“a”需要小于1且大于2(同时成立),这是不可能的。
我建议您切换到“while”循环:
while (a != 1 && a != 2)
但是,如果您仍然想使用“for”循环,请尝试更改条件为:
a < 1 || a > 2
希望能对您有所帮助,如果有问题,请告诉我。
英文:
For this type of loop i would suggest using "while" loop instead of "for" loop (you said you want to use while loop but actually used for loop).
"for" loops are used when you know in advance the number of times you want the loop to run.
"while" loops are used when you don't know how many times the loop will run - which in your case depends on the input from the user.
Also, in your "for" loop you set the condition to be that "a" needs to be lower then 1 AND bigger then 2 (at the same time) which is impossible.
I suggest you switch to "while" loop:
while (a != 1 && a != 2)
but if you still want to use the "for" loop try changing the condition to be:
a < 1 || a > 2
Hope this helps, let me know.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论