2013-02-26 22 views
2

我試圖獲取用戶輸入,但我得到 illegal start of expression在:我有錯誤與用戶開關輸入「表達的非法啓動」

public static String askTheUser() throws IOException 

完整代碼:

編輯:取得了大部分的變化你們提出所以現在我有這樣的:

import java.io.BufferedReader; 

public class Driver 
{ 

public static void main(String[]args) 
{ 
    Dice dice; 
    Craps craps; 

    userResponse = askTheUser(); 
    while(userResponse.equalsIgnoreCase("yes")) 
    { 
     craps = new Craps(); 
     while(!craps.gameOver()) 
     { 
      craps.roll(); 
      //print out results of roll 
     } 
     //print out game results: if(craps.gameWon()... 
     userResponse.askTheUser(); 
    } 
} 

public static String askTheUser() throws IOException 
{ 
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); 
    String data; 

    System.out.print("Want to play craps? Yes or No"); 
    data = dataIn.readLine(); 
    if(data.equals("y") || data.equals("yes")) 
    { 
     return "yes"; 
    } 
    else 
    { 
     return "no"; 
    } 
} 
} 

但是我在仍然得到。那麼我可能會錯過一個我不知道的進口嗎?

回答

10

您聲明askTheUser方法裏面主要方法rip it列出了主要方法。

public static void main(String[]args) 
    { 
     //code that goes inside main 
    } 
    public static String askTheUser() throws IOException 
    { 
     // code that goes in askTheUser 
    } 
+1

而且,你永遠不聲明一下類鍵盤。 – aglassman 2013-02-26 21:35:56

+0

@aglassman true ... :) – PermGenError 2013-02-26 21:37:40

+0

好的,謝謝我這麼做,現在我想我錯過了導入或其他東西,因爲我仍然收到錯誤。我有'java.import.io.BufferedReader'。我需要別的嗎? – mbridges 2013-02-26 22:04:16

0

不能在裏面Java方法寫方法。 但是你可以在同一個類中有很多方法。

爲什麼?由於Java規格..你根本不被允許這樣做。

請注意,您可以在其他方法下使用方法anonymous inner class

0

我不認爲keyboard.readline()的作品?

使用:

InputStreamReader converter = new InputStreamReader(System.in); 
BufferedReader in = new BufferedReader(converter); 
in.readLine(); // Convert to string or int needed! 
相關問題