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";
    }
}
}

但是我仍然cannot find symbolpublic static String askTheUser() throws IOException. 那么我可能会错过一个我不知道的进口吗?

4

3 回答 3

10

askTheUsermain 方法中声明了方法rip it退出主要方法。

   public static void main(String[]args)
   {
       //code that goes inside main
   }
   public static String askTheUser() throws IOException
   {
       // code that goes in askTheUser
   }
于 2013-02-26T21:32:57.670 回答
0

不能Java. 但是你可以在同一个类中有很多方法。

为什么?由于Java规格..您根本不允许这样做。

请注意,您可以anonymous inner class在另一种方法下使用 with 方法。

于 2013-02-26T21:33:23.630 回答
0

而且我认为keyboard.readline() 不起作用?

采用:

InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
in.readLine(); // Convert to string or int needed!
于 2013-02-26T21:37:21.730 回答