2016-12-16 27 views
0

ゲームの簡単なバージョンを作成しようとしています。シャット・ザ・ボックスは、プレイヤーが交代するサイコロのゲームです。コードを実行すると、 'StdRandom'と 'scan'でエラーが発生します。誰でも手伝うことができますか? ここにコードがあります。私はそれを実行するとこのコードを実行しているときにエラーが発生しました。プログラムを終了します。

public class test { 

public static void main(String[] args) { 
    System.out.println("Shut the Box"); 
    System.out.println("123456789"); 
    System.out.println("Your goal is to close all of them, leaving the game in this state:"); 

    boolean[] close = new boolean[10]; 
    for (int i = 1; i <= 9; i++) { 
     System.out.print(i); 
    } 
    System.out.println(); 
    int score = 45; 
    while (true) { 
     int roll = StdRandom.uniform(1, 7); 
     if (close[7] && close[8] && close[9]) { 
      System.out.println("7, 8, and 9 are close,you can only roll one dice"); 
     } else { 
      roll += StdRandom.uniform(1, 7); 
     } 
     System.out.println("You rolled " + roll + "."); 
     System.out.print("How many levers will you close? "); 
     int count = scan.readInt(); 
     if (count == 0) { 
      System.out.println("Game over. Your final score is " + score + "."); 
      return; 
     } 
     System.out.println("Enter the numbers of the levers you want to close."); 
     int total = 0; 
     for (int i = 0; i < count; i++) { 
      int n = scan.readInt(); 
      if (close[n]) { 
       System.out.println("That lever is already close. You forfeit the game."); 
       return; 
      } 
      close[n] = true; 
      score -= n; 
      total += n; 
     } 
     if (roll != total) { 
      System.out.println("Those numbers don't add up to " + roll + "Game over"); 
      return; 
     } 
     for (int i = 1; i <= 9; i++) { 
      if (close[i]) { 
       System.out.print("-"); 
      } else { 
       System.out.print(i); 
      }   
     } 
     System.out.println(); 
     if (score == 0) { 
      System.out.println("You've shut the boxes! you win!"); 
      return; 
     } 
    } 
} 

} 

私は、このエラーにランダムな数字については

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    StdRandom cannot be resolved 
    StdRandom cannot be resolved 
    scan cannot be resolved 
    scan cannot be resolved 

at test.main(test.java:15) 
+1

問題は:グリッドはタイプ –

+0

に解決できません申し訳ありませんが、私はエラーを更新しました –

+0

'scan'変数は決して宣言されません。また 'StdRandom'は別のクラスですか?そうであれば、別のパッケージからのものであれば、import文が見つからないことがあります。 – Berger

答えて

0

を取得し、私はこれを使用します。

Random r = new Random(); 
int roll = r.next(6) + 1; 

があなたのスキャンのためにGetting random numbers in Java

を参照してください、あなたが持っていますまずそれを宣言して(バーガー氏はそれを指摘した)、それをあなたの文書の一番上にインポートします。あなたのコード(宣言と初期化)内部

import java.utils.Scanner; 

Scanner sc = new Scanner(System.in); 
int count = sc.nextInt(); 

あなたのコードはまた、潜在的なバグのカップルを持っているHow to read integer value from the standard input in Java

、私のようにJavaの上でいくつかのスタック対象を読むことをお勧めします参照してください。たくさんあります!がんばろう!

関連する問題