【问题标题】:Having trouble with this switch inside my while loop在我的 while 循环中使用此开关时遇到问题
【发布时间】:2014-01-16 19:33:54
【问题描述】:

所以我试图复制我以前玩过的这款游戏。在这个游戏中,您会看到一个数字,并且有一个隐藏的数字。您需要猜测这个隐藏的数字是更小、更大还是与显示的数字相同。我在让输入正常工作时遇到问题。我似乎无法让 switch 语句正常工作。我的扫描仪也有问题。虽然它在 while 循环的外部它可以工作,但在它内部却没有。

import java.util.Scanner;
import java.util.Random;
public class Jamey {

    /**
     * @param args
     */
    public static void main(String[] args) {

            //This will give us the first random shown number
        Random yourRandom = new Random();
        int y = 1+yourRandom.nextInt(10);

        //Here is the introduction text
        System.out.println("Welcome to the guessing game!");
        System.out.println("The objective of this game is simple.");
        System.out.println("You will be shown one of two numbers which range between one and ten.");
        System.out.println("You have to gues if the number shown is larger, smaller, or equal to the hidden number.");
        System.out.println("If you believe the number you see is larger enter 1.");
        System.out.println("If you believe the number you see is smaller enter the 3.");
        System.out.println("If you believe the number you see is the same enter the 2.");
        System.out.println("Good luck, your number is "+y+".");

        boolean isDone = false;
        while(isDone=false){            

            //This allows the user to guess
            Scanner guess = new Scanner(System.in);
            int g = guess.nextInt();

            //This will help us to keep score later.
            int score = 0;

            //This will give us the new random number
            Random newRandom = new Random();
            int n = 1+newRandom.nextInt(10);

            //This will give us the new hidden number
            Random hiddenRandom = new Random();
            int r = 1+hiddenRandom.nextInt(10);

            //This is to allow multiple different inputs
            switch(score){
            case 1 :
                score +=1;
                if(y>r){
                    System.out.println("Correct");
                    System.out.println("Your new number is "+n+".");
                }
                else{
                    score +=1;
                    System.out.println("Inccorect, your overall score was "+score+".");
                    isDone = true;
                }
                break;

            case 2 :
                score +=1;
                if(y==r){
                    System.out.println("Correct");
                    System.out.println("Your new number is "+n+".");
                }
                else{
                    System.out.println("Inccorect, your overall score was "+score+".");
                    isDone = true;
                }
                break;

            case 3 :
                score +=1;
                if(y<r){
                    System.out.println("Correct");
                    System.out.println("Your new number is "+n+".");
                }
                else{
                    System.out.println("Inccorect, your overall score was "+score+".");
                    isDone = true;
                }
                break;
            default:
                System.out.println("Invalid input.");
                isDone = true;
                break;

            }       
        }
    }
}

【问题讨论】:

    标签: java while-loop switch-statement java.util.scanner


    【解决方案1】:

    您的 while 语句正在使用赋值

    while(isDone=false) // Incorrect
    while (isDone == false) OR while(!isDone) // Better
    

    注意== 而不是=。您没有进行比较,只是将 isDone 设置为 false 每次迭代。

    您的 switch 语句失败,因为您正在检查 score 变量而不是猜测变量。

    switch(score) // Incorrect
    switch(g) // Better
    

    此外,当您只需要创建一个实例时,您正在创建大量 Random 对象,然后相应地命名每个变量。例如

    Random rand = new Random();
    int yourRandom = 1 + rand.nextInt(10);
    int newRandom = 1 + rand.nextInt(10);
    int hiddenRandom = 1 + rand.nextInt(10);
    

    【讨论】:

    • while(!isDone) 看起来更好
    • 我从未见过有人写 if (b == true) 或类似的东西,每个人都写 if (b)。有时新手会这样做。
    • 感谢您的帮助。我不敢相信事情就这么简单。
    • 现在我得到的只是从我的默认语句中获得“无效输入”响应。有什么帮助吗?
    • @user3203973 更新了我的答案以包含 switch 语句问题。
    【解决方案2】:

    while(isDone=false){ 应该 while(isDone==false){ 甚至更好 while(!isDone){

    【讨论】:

    • 谢谢,小事总能吸引我。
    • 现在我得到的只是从我的默认语句中获得“无效输入”响应。有什么帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多