[英]Do-While Loop not looping - Java
I am having a problem with my java code below.我的 java 代码有问题。 I want the loop to stop when the number "-100" but it stops as soon as you enter any number.
我希望循环在数字“-100”时停止,但只要您输入任何数字,它就会停止。 I'm just learning how to use java so there could be plenty of mistakes here.
我只是在学习如何使用 java 所以这里可能会有很多错误。
public static void main(String[] args){
Scanner keyboard = new Scanner (System.in);
String num = "";
do {
System.out.println("Enter a number: ");
int n = keyboard.nextInt();
System.out.println("The number you entered is: " +n);
System.out.println("------------------------");
} while ("-100".equals(num));
}
}
num
is always the empty string, because you never change the value of num
. num
始终是空字符串,因为您永远不会更改num
的值。 You update n
.你更新
n
。 Which is what I would base the loop on.这是我循环的基础。 Like,
喜欢,
Scanner keyboard = new Scanner(System.in);
int n;
do {
System.out.println("Enter a number: ");
n = keyboard.nextInt();
System.out.println("The number you entered is: " + n);
System.out.println("------------------------");
} while (n != -100);
That is, do loop while n
is not -100
.也就是说,do loop while
n
is not -100
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.