Java while循环尽管条件为假也不会终止

时间:2020-09-27 16:38:30

标签: java loops

我想创建一个名为Crap的游戏。我面临的问题是尽管条件设置为false(通过在询问用户是否要继续播放时输入“ no”),主while循环仍未结束。这里有什么问题?我认为问题出在哪里。

import java.util.Scanner;

public class crap {

    public static void main(String[] args) {
        System.out.println("You are about to play the game of Crap. Press Enter to continue.");
        
        Scanner enter = new Scanner (System.in);
        String hitEnter = enter.nextLine();
        Scanner input = new Scanner(System.in);
        String proceed = "yes";
            
        // This loop does not exit even if proceed == "no"
        while (proceed != "no"){
            
            int playerPoint;
            int firstDice = 1 + (int) (Math.random() * 10) % 6;
            int secondDice = 1 + (int) (Math.random() * 10) % 6;
            int throwSum = firstDice + secondDice;
            
            if (throwSum == 7 || throwSum == 11) {
                System.out.println("Congratulations! You win on your first roll! You rolled " 
            + firstDice + " and " + secondDice + " for a total of " + throwSum);
            }
            else if (throwSum == 2 || throwSum == 3 || throwSum == 12) {
                System.out.println("Sorry, you crapped out, you lose! You rolled " + firstDice +
                " and " + secondDice + " for a total of " + throwSum);
            } else {
                playerPoint = throwSum;
                System.out.println("You rolled " + firstDice + " + " + secondDice + " which is "
                + playerPoint);
                System.out.println("Your point is " + playerPoint + " now. Good luck.");
                
                while (throwSum != 7 ) {
                    firstDice = 1 + (int) (Math.random() * 10) % 6;
                    secondDice = 1 + (int) (Math.random() * 10) % 6;
                    throwSum = firstDice + secondDice;
                    
                    if (throwSum != 7) {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        
                        if (throwSum == playerPoint) {
                            System.out.println("Congratulations! You win. You reached your point.");
                        break;
                        }
                        System.out.println("Your point is " + playerPoint + ". Good luck.");
                    }
                     else {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        System.out.println("Sorry, you crapped out, you lose."); 
                        System.out.println("You rolled 7 before reaching your point.");
                        break;
                    }
                }       
            }
            System.out.println("Do you want to play again? yes/no: ");
            
                 // even if user enters "no" the loop does not exit     
            proceed = input.nextLine(); 
        }
        System.out.println("Thanks for playing.");
        enter.close();
        input.close();
    }
}

2 个答案:

答案 0 :(得分:1)

您使用了错误的运算符。 !=检查左侧和右侧的两个对象是否相同(例如,两个变量引用内存中的同一对象)。

您必须写while (! "no".equals(proceed) )

我不是故意写while (! proceed.equals("no") ),因为如果字符串为空,那么您将得到一个NullPointerException。

我不确定Scanner.readLine()是否返回空字符串,因此在这里可能没有区别。但是,像我第一个示例一样,以“反向”方式编写代码通常更安全。

答案 1 :(得分:0)

对于字符串,==运算符用于比较给定字符串的引用,具体取决于它们是否引用相同的对象。当使用==运算符比较两个字符串时,如果字符串变量指向同一个Java对象,它将返回true。否则,它将返回false。

您必须使用.equals(String)来比较字符串,如下所示:

import java.util.Scanner;

public class crap {

    public static void main(String[] args) {
        System.out.println("You are about to play the game of Crap. Press Enter to continue.");
        
        Scanner enter = new Scanner (System.in);
        String hitEnter = enter.nextLine();
        Scanner input = new Scanner(System.in);
        String proceed = "yes";
            
        // This loop does not exit even if proceed == "no"
        while (!proceed.equals("no")){
            
            int playerPoint;
            int firstDice = 1 + (int) (Math.random() * 10) % 6;
            int secondDice = 1 + (int) (Math.random() * 10) % 6;
            int throwSum = firstDice + secondDice;
            
            if (throwSum == 7 || throwSum == 11) {
                System.out.println("Congratulations! You win on your first roll! You rolled " 
            + firstDice + " and " + secondDice + " for a total of " + throwSum);
            }
            else if (throwSum == 2 || throwSum == 3 || throwSum == 12) {
                System.out.println("Sorry, you crapped out, you lose! You rolled " + firstDice +
                " and " + secondDice + " for a total of " + throwSum);
            } else {
                playerPoint = throwSum;
                System.out.println("You rolled " + firstDice + " + " + secondDice + " which is "
                + playerPoint);
                System.out.println("Your point is " + playerPoint + " now. Good luck.");
                
                while (throwSum != 7 ) {
                    firstDice = 1 + (int) (Math.random() * 10) % 6;
                    secondDice = 1 + (int) (Math.random() * 10) % 6;
                    throwSum = firstDice + secondDice;
                    
                    if (throwSum != 7) {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        
                        if (throwSum == playerPoint) {
                            System.out.println("Congratulations! You win. You reached your point.");
                        break;
                        }
                        System.out.println("Your point is " + playerPoint + ". Good luck.");
                    }
                     else {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        System.out.println("Sorry, you crapped out, you lose."); 
                        System.out.println("You rolled 7 before reaching your point.");
                        break;
                    }
                }       
            }
            System.out.println("Do you want to play again? yes/no: ");
            
                 // even if user enters "no" the loop does not exit     
            proceed = input.nextLine(); 
        }
        System.out.println("Thanks for playing.");
        enter.close();
        input.close();
    }
}