我是编程新手,所以认为我是一个很棒的新手。困境:每次我回答“是”时,我都想重复我的代码。我确实使用了“do while 循环”,因为语句首先出现,布尔条件最后应该被评估。
编码:
import java.util.Scanner;
class whysoserious {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
do{
String go = YES;
int setone = 0;
int settwo = 0;
System.out.println("Enter two numbers: ");
setone = sc.nextInt();
settwo = sc.nextInt();
int set = setone + settwo;
System.out.println("What is " +setone+ " + " +settwo+ " ? ");
int putone = 0;
putone = sc.nextInt();
if (putone == set)
System.out.println("Correct!");
else
System.out.println("Wrong Answer - The correct answer is: "+set+"");
System.out.println("Continue?");
cont = sc.nextLine();
} while (cont == go);
}
}
我被要求添加 CMD 提示行。
- C:\test>javac whysoserious.java whysoserious.java:15: 找不到符号 symbol : variable YES location: class whysoserious String go = YES; ^ whysoserious.java:38: 找不到符号 symbol: 变量 cont 位置: class whysoserious cont = sc.nextLine(); ^ whysoserious.java:39: 找不到符号 symbol: variable cont location: class whysoserious } while (cont == go); ^ whysoserious.java:39: 找不到符号 symbol : variable go location: class whysoserious } while (cont == go); ^ 4 个错误
每次我尝试编译它时总是会出错。这背后的知识是,我想在用户每次输入 Yes 或 No 以继续时重复代码。观察代码在没有 do { 的情况下工作。在这一点上,我坚持做一段时间。