2010-11-20 3 views
0

私のCOSC 117クラス用のJavaでプロジェクトをやっていますが、私のブール型の "done"が決して読み込まれないというエラーが表示されます。これを修正する方法を見つけることはできません。ありがとうございました。ブール型は読み込まれませんか?

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

あなたは、あなたのブールが「読み込まれていない」よりも***の方法で大きな問題を抱えています。インデント、あなたはその言葉を知っていますか?これは、インデントあたり1スペース以上の** **を意味します。また、同じ行に2つの '}'があってはならないことを意味します。私は今度はそれを修正しました。次にコードブロックを使用します。 –

+0

ありがとうございますが、最初のロールが2 3 7または12でない場合にこのプログラムを実行すると、最初の数字または7をロールするまでロールし続けるのではなく、単に「タイプして再度再生する」と表示されます。 –

+0

ところでなぜダブルスを使用していますか? :) – irrelephant

答えて

0

私はそれが警告、ないエラーだと思います。しかし、最初に値を読み取ることなくコード内で後でfalseに設定するので、変数を宣言したときにdoneの値をfalseに設定したときに値が使用されないというメッセージが表示されていると思います。これは間違いの証拠となることが多いため、有益な警告です。それでもコンパイルと実行ができるはずです。

0

ないあなたの元の質問への答えが、この行:

while (done=false) 

は、おそらくJavaで

while (done==false) 

する必要があります=オペレータが割り当てに使用され、==演算子は等価性のために使用されています比較。

1

while (done=false)からwhile (!done)に変更してください。

最初の割り当てが実行されます。この時

+0

ありがとうございました、プログラムは今実行されます。しかし、私はどのようにプログラムを変更するので、ユーザーはループの代わりに自動的に "はい"の文字列を入力する必要がありますか? –

+0

@Ryan、あなたのコードに表示されているものから、すでにwhile(answer2.equalsIgnoreCase( "yes")); ' – irrelephant

+0

私はダイスをロールするたびに"ロールをシミュレートする "かそのようなものですが、私はそれをどうやって行うのか分かりません。今、プログラムは自動的にそれを行います。これは、私がルールを読んでいるかどうかを言うとすぐに実行される例です。 –

4

ルック:

while (done=false) 

はここにどんな問題を参照してください?これは宿題なので、私はあなたに話しません。

関連する問題