2013-10-02 57 views
0

當我編譯我的程序,我得到一個錯誤的說法,的Java myInput.readLine錯誤

找不到符號

符號:變量myInput

位置:類diceGame

我已經看着我的代碼,似乎無法找到讀取行中的錯誤。這是什麼意思?爲什麼會發生此錯誤?

import java.io.*; 
import java.lang.*; 
import java.util.*; 

public class diceGame 
{ 
    public static void main(String[] args) throws IOException { 

    pairOfDice dice; 

    dice = new pairOfDice(); 

    playerGame player; 

    player = new playerGame(); 

    int rollCount = 0; 

    int holdB = 0; 
    do { 
     dice.roll(); // Roll the first pair of dice. 
     System.out.println("Dice 1: " + dice.getDiceA() + "\n" + "Dice 2: " + dice.getDiceB() + "\n" + "The total is: " + dice.getTotal()); 
     System.out.println("Do you want to hold the value of the dice? Press 0 to hold none/ 1 to hold die 1/ 2 to hold die 2/ 3 to hold both"); 
     String hold = myInput.readLine(); 
     int holdA = Integer.parseInt(hold); 
     if (holdA == 0){ 
     } 
     if (holdA == 1){ 
     player.getHoldA(); 
     player.setHoldA(dice.getDiceA()); 
     System.out.println("Value of dice A is held"); 
     } 
     if (holdA == 2){ 
     player.setHoldB(dice.getDiceB()); 
     System.out.println("Value of dice B is held"); 
     } 
     if (holdA == 3){ 
     player.setHoldA(dice.getDiceA()); 
     System.out.println("Value of dice A is held"); 
     player.setHoldB(dice.getDiceB()); 
     System.out.println("Value of dice B is held"); 
     break; 
     } 
     rollCount++; 
    } 
    while (dice.getTurns() <= 3); 
    } 
} 

回答

2

這意味着,在這條線:

String hold = myInput.readLine(); 

它不知道什麼myInput是,因爲你從來沒有定義它。

最有可能你想添加一個BufferedReader周圍System.in,然後從上面寫着:

BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
String nextLine = input.readLine(); 
+0

謝謝,我忘了補充一個緩衝的讀者 – user481710