2016-12-16 85 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一些堆栈主题有很多!祝你好运!