0

我是编程新手,所以认为我是一个很棒的新手。困境:每次我回答“是”时,我都想重复我的代码。我确实使用了“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 { 的情况下工作。在这一点上,我坚持做一段时间。

4

4 回答 4

7

Multiple problems:

  • you never declare cont or YES so the compiler doesn't know what they are

  • you declare go within the loop, so its out of scope in the while expression

  • comparing strings with == doesn't do what you need here -- you might have two different strings with the same contents. Use equals instead.

The error messages from the compiler should tell you about the first two -- pay attention to what the compiler is saying.

easiest fix: add String cont; before do, get rid of go and make the test while ("YES".equals(cont)); This will still exit if the user enters "yes" or "Y" or " YES" or some other variation, however.

于 2010-12-01T17:55:09.857 回答
3

The line:

 String go = YES;

should read:

String go = "YES";

Otherwise, the compiler will think that YES is some variable that it doesn't know about, and proceed to freak out.

于 2010-12-01T17:55:12.257 回答
1

添加到@Ronnie Howell 所说的内容......使用equals()字符串相等的方法。

while (cont == go);

应该读...

while (cont.equals(go));

PS ...我看不到您在哪里定义cont. 请在do循环外声明它。

于 2010-12-01T18:01:10.833 回答
0

以下作品:

import java.util.Scanner;

public class WhySoSerious {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String cont;
        String go = "YES";

        do {

            System.out.println("Enter two numbers: ");

            int setone = sc.nextInt();
            int settwo = sc.nextInt();
            int set = setone + settwo;


            System.out.println("What is " + setone + " + " + settwo + " ? ");
            int 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.next();
        } while (cont.equalsIgnoreCase(go));
    }
}
于 2010-12-02T11:46:06.310 回答