Java 我有一个;“表达式开头非法”;用户输入错误
我正在尝试获取用户输入,但是Java 我有一个;“表达式开头非法”;用户输入错误,java,Java,我正在尝试获取用户输入,但是 表达式的开头非法位于: public static String askTheUser() throws IOException 完整代码: 编辑:我已经做了你们建议的大部分更改,所以现在我有以下内容: import java.io.BufferedReader; public class Driver { public static void main(String[]args) { Dice dice; Craps craps;
表达式的开头非法
位于:
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";
}
}
}
然而,我仍然在
公共静态字符串askTheUser()中得到找不到符号
。所以我可能错过了一个我不知道的导入?您在主方法中声明了askTheUser
方法<编码>将其从主方法中删除
public static void main(String[]args)
{
//code that goes inside main
}
public static String askTheUser() throws IOException
{
// code that goes in askTheUser
}
不能在
Java
中的方法内部编写方法。
但是在同一个类中可以有许多方法
为什么??因为Java
规范。。你根本不被允许这样做
请注意,您可以有一个
匿名内部类
,其中包含另一个方法下的方法。我认为keyboard.readline()不起作用
使用:
另外,你从来没有声明什么是类键盘。好吧,谢谢,我这么做了,现在我想我错过了一个导入或其他东西,因为我仍然会出错。我有
java.import.io.BufferedReader
。我还需要什么吗?@mbridges,正如其他人已经指出的那样。键盘的类型是什么??我想您需要声明BufferedReader keyboard=new BufferedReader(Reader)
@PremGenError我去掉了BufferedReader键盘部分,并更新了我的代码。@mbridges正确地计算大括号。您必须将askTheUser()
与publicstaticvoidmain(String[]args)分开
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
in.readLine(); // Convert to string or int needed!