【发布时间】:2014-03-06 17:03:03
【问题描述】:
我正在为 Intro to CS 做一个学校项目,但我无法发布我的代码,因为我们的老师让我们无法从 StackOverflow 中检查代码。我的问题是我的代码格式为:
while (condition){
Do something;
if (new condition){
Do something else;
Wait for input;
Depending on input make while's condition false;
}
当 if 语句被评估并执行其他操作时,此代码应等待输入。但是,我的代码不会等待“等待输入”步骤,而是直接进入“做某事”步骤。这是一些代码。感谢您的帮助。
while (inum != 102) {
System.out.println("Enter a number between 1 and 100: ");
inum = input.nextInt();
else if (inum == 101) {
System.out.println("Are you sure you want to quit?");
confirm = input.nextLine();
if (confirm == "yes") {
inum = 102;
}
}
当我输入 101 时,这里的代码给了我这个: 你确定你要退出吗? 输入一个介于 1 到 100 之间的数字:
*代码不等待
confirm = input.nextLine();
if (confirm == "yes") {
inum = 102;
}
步骤。
【问题讨论】:
-
@SotiriosDelimanolis 和 else-if 没有任何以前的
if显示在代码中 -
SO 上最常被问到的问题,看来... 将
if (confirm == "yes")替换为if ("yes".equals(confirm)) -
更改为 ("yes".equals(confirm)) 后仍然得到相同的响应
-
您的牙套和 else if 的位置有些有趣。你能检查你的示例编译和必要的更新吗?
标签: java loops if-statement while-loop