布尔类型没有读入?

时间:2010-11-20 23:42:01

标签: java

我正在用Java编写一个用于我的COSC 117类的项目,我的代码中出现错误,说我的布尔类型“完成”从未被读入。我无法弄清楚如何解决这个问题。谢谢。

import java. util.Scanner;
public class PlayCraps {

     /**
      * This program will play the game of craps. The user will roll the dice and if it is a 7 or 11,
      * they win. If the first roll is a 2, 3 or 12; however, they lose. If it is neither, the user
      * keeps rolling until they roll their first number again or a 7. If they roll their first number
      * they win, if they roll a 7, they lose. The program will then ask the user if they want to play 
      * again
      * Programmer: Ryan Mitchell
      * Date: November 19, 2010
      */

     public static void main(String[] args) {
        Scanner keyboard = new Scanner (System.in);
        String answer1;
        double firstRoll;
        double makeRoll;
        int totalWins=0;
        int totalLoses=0;
        String answer2;
        boolean done=false;

        System.out.println("If you would like to see the rules of craps type yes, otherwise type no");
        answer1 = keyboard.next();

        if (answer1.equalsIgnoreCase("yes")){
            System.out.println("You will roll the dice at least one time. If the first roll is a 7 or 11 you win");
            System.out.println("If the first roll is a 2, 3 or 12; however, you lose.") ;
            System.out.println("If it is neither, you keep rolling until you roll the first number again or a 7. ");
            System.out.println("If you roll the first number you win, if you roll a 7, you lose. ");
            System.out.println("The program will then ask the you if you want to play again");
        }
        System.out.println("Let's play craps");

        do 
        {
            firstRoll=Dice.roll();
            System.out.println("Your first roll is " + firstRoll);
            if (firstRoll == 7|| firstRoll==11) 
            {
                System.out.println("Winner!");
                totalWins++;
            }
            else if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12) 
            {
                System.out.println("Loser!");
                totalLoses++;
            }
            else
            {
                done = false;
                while (done=false)
                { 
                    System.out.println("Now you have to roll your first number before you roll a 7");
                    makeRoll=Dice.roll();
                    System.out.println("You rolled a " + makeRoll);

                    if (makeRoll == firstRoll)
                    {
                        done = true;
                        totalWins++;
                        System.out.println("Winner!");
                    }
                    else if (makeRoll==7)
                    {
                        done = true;
                        totalLoses++;
                        System.out.println("Loser!");
                    }
                }
            }
            System.out.println("Type yes to play again");
            answer2=keyboard.next();

        } while (answer2.equalsIgnoreCase("yes"));

        System.out.println("You won " +totalWins +" games");
        System.out.println("You lost " +totalLoses +" games");
    }
}

public class Dice {
    public static double roll() {
        double num1 = Math.floor(Math.random()*6+1);
        double num2 = Math.floor(Math.random()*6+1);
        return num1 + num2; 
    }
}

4 个答案:

答案 0 :(得分:4)

看看这个:

while (done=false)

在这里看到任何问题?我不会告诉你,因为这是家庭作业。

答案 1 :(得分:1)

while (done=false)更改为while (!done)

第一个执行任务。

答案 2 :(得分:0)

我认为这是警告,而不是错误。但是,我相信该消息表明在声明变量时第一次将done的值设置为false时,由于您在代码中稍后再次将其设置为false而未读取该值,因此未使用该值第一个值。这通常是错误的证据,因此是有用的警告。你仍然可以编译并运行。

答案 3 :(得分:0)

不是你原来问题的答案,而是这一行:

while (done=false)

应该是

while (done==false)

在java中,=运算符用于赋值,而==运算符用于等价比较。