2016-12-16 73 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'是另一個類嗎?如果是這樣,如果它來自另一個包,則可能會缺少導入語句。 – Berger

回答

0

對於隨機數字,我會用這樣的:

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

Getting random numbers in Java

對於您的掃描,你有首先聲明它(Berger指出)並將其導入到文檔的頂部。

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一些堆棧主題有很多!祝你好運!

相關問題