2010-11-20 6 views
0

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; 
    } 
} 
+0

당신은 당신의 bool이 읽지 않는 ***보다 큰 문제를 가지고 있습니다. 들여 쓰기, 그 말 알아? 즉, 들여 쓰기 당 ** 공간 ** 이상을 의미합니다. 그것은 또한 같은 줄에 두 개'''가 있어서는 안된다는 것을 의미합니다. 다음 번에는 코드 블록을 사용하여 그것을 고쳤습니다. –

+0

감사합니다.하지만 첫 번째 롤이 2 3 7 또는 12가 아닌 경우이 프로그램을 실행할 때 첫 번째 숫자 또는 7을 굴릴 때까지 롤링을 계속하는 대신 "다시 입력하려면"라고 말합니다. –

+0

그건 그렇고 왜 복식을 쓰고 있니? :) – irrelephant

답변

0

나는 그것이 경고하지 오류 생각합니다. 그러나 변수를 선언 할 때 처음으로 done의 값을 false로 설정하면 나중에 첫 번째 값을 읽지 않고 코드에서 다시 false로 설정했기 때문에 값이 사용되지 않는다는 메시지가 나타납니다. 이것은 종종 실수의 증거이므로 도움이되는 경고입니다. 당신은 여전히 ​​컴파일하고 실행할 수 있어야합니다.

0

아닌 원래의 질문에 대한 대답하지만,이 라인은 :

while (done=false) 

아마도 = 연산자는 할당에 사용되며 == 연산자는 동등성에 사용되는 자바

while (done==false) 

해야한다 비교.

1

while (done=false)while (!done)으로 변경하십시오.

첫 번째 할당이 수행됩니다. 이것

+0

감사합니다. 지금 프로그램이 실행됩니다. 그러나 사용자가 자동으로 루프 반복 대신 "예"문자열을 입력해야하므로 프로그램을 어떻게 변경합니까? –

+0

@Ryan, 당신의 코드에서 볼 수 있듯이, 이미'while (answer2.equalsIgnoreCase ("yes")); ' – irrelephant

+0

라고 말하기 위해 주사위를 굴릴 때마다 말하기를 프로그램하고 싶습니다. 롤 (roll)을 시뮬레이트하는 것 "또는 그런 식으로 생각하지만, 어떻게 해야할지 모르겠다. 지금 당장 프로그램이 자동으로 처리합니다. 이것은 내가 규칙을 읽으려는 것을 말하거나하지 말고 바로 실행하는 예입니다. –

4

봐 :

while (done=false) 

어떤 문제 여기를 참조하십시오? 이게 숙제 이니까 너 한테 말하지 않을거야.

관련 문제