1

CS クラスの宿題に問題があります。スロット マシンを作成することになっているのですが、続行できません。スキャナーの nextLine メソッドが行をスキップしてそこにあったものを保持していることがわかりますが、何も入力できず、ただ終了するだけです。プログラム。また、このメソッドを分割して 30 行以下にする必要があります。これは、私の教授が必要とするフォーマットのためです。賞金の現在の合計も記録する必要がありますが、ジャックポットを獲得する方法がわかりません。

/**
 * ProgrammingAssignment2.java
 * 
 * @author Corey Goff 
 * @version March 2013
 */
import java.util.Random;
import java.util.Scanner;
/**
* This class simulates a slot machine.
*/
public class SlotMachine
{
   /**
    * This is the main method.
    * 
    * @param args
    */
   public static void main(String[] args)
   {
       Scanner keyboard = new Scanner(System.in);
       Random random = new Random();
       String cont = "n";
       char answer;
       int coin = 0;
       int totalEntered = 0;
       int a;
       int b;
       int c;
       int n;
       int amountWon = 0;
       int dubs = coin * 2;
       int trips = coin * 4;

       while (cont.equals("n"))
       {
           a = random.nextInt(6);
           b = random.nextInt(6);
           c = random.nextInt(6);
           n = random.nextInt(991) +10;
           totalEntered += coin;
           System.out.println("How much would you like to bet? ");
           coin = keyboard.nextInt();

           switch (a) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           switch (b) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           switch (c) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           if (a != b && a != c && b != c)
           {
               System.out.println("You have won $0");
           }
           else if (a == b || a == c || b == c)
           {
               System.out.println("Congratulations, you have won $" + dubs);
                  amountWon += dubs;
           }
           else if (a == b && a == c && a != 0)
           {
               System.out.println("Congratulations, you have won $" + trips);
                  amountWon += trips;
           }
           else if (a == 0 && b == 0 && c == 0)
           {
               System.out.println("Congratulations! You have won the jackpot of $" 
                   + (coin * n));

           }

           System.out.println("Continue? y/n ");
           cont = keyboard.nextLine();
       }
   }
}
4

2 に答える 2

2

keyboard.nextInt() を呼び出した後、keyboard.nextLine() を呼び出して改行文字をバッファーにダンプする必要があります。nextInt() は、int の一部になり得ないものが見つかるまで読み取り、int の後のすべてをバッファーに残します。

詳細については、この投稿を参照してください:文字列の読み取り next() および nextLine() Java

于 2013-03-27T18:59:44.157 に答える
0

コードを機能させるには (少し変更して) keyboard.next();:keyboard.nextLine();

最初の cont = "n" を cont = "y" に変更し、while ループで if (cont.equals("y")) をチェックします。

これは簡単な修正です。効率を上げたり標準に合わせたりするために多くの変更を加えることができますが、詳細には触れません。

30 行未満の要件については、まずランダム ジェネレーターを 1 つだけ使用して、1 つの構造で 3 つのフルーツすべてを生成します。果物ごとに個別の乱数発生器が必要な理由を自問してみてください。なぜ果物ごとに新しい切り替えを行うのですか? 1 つのランダム対 3 つのランダム ジェネレーターは、果物に同じランダム性を生成します。

于 2013-03-27T19:04:14.837 に答える