Java 否则没有if错误
这是我的代码Java 否则没有if错误,java,Java,这是我的代码 public static void play(Player player) { Scanner localScanner = new Scanner(System.in); while(localScanner.hasNextLine()){ String input = localScanner.nextLine(); if(input.equals("quit")) { System.out.println("Game
public static void play(Player player) {
Scanner localScanner = new Scanner(System.in);
while(localScanner.hasNextLine()){
String input = localScanner.nextLine();
if(input.equals("quit")) {
System.out.println("Game over, Good bye.");
return;
}
if(input.contains("north")){
buildWorld(player, player.moveTo().connectNorth());
} else if(input.contains("south")){
buildWorld(player, player.moveTo().connectSouth());
} else if(input.contains("east")){
buildWorld(player, player.moveTo().connectEast());
} else if(input.contains("west")){
buildWorld(player, player.moveTo().connectWest());
} else {
String contents;
if (!contents.equals(""))
System.out.println("There is:\n" + contents);
else{
System.out.println("This room is empty.");
} else if(input.startsWith("pickup")){ // <-- This is the 'orphan' else
contents = input.substring(8);
if(player.moveTo().moveTo(contents)){
Object localObject = player.connectWest(contents);
if(localObject !=null)
System.out.println("You have picked up " +localObject);
else
System.out.println("You have too much damage, Game over.");}
else {
System.out.println("There is no " +contents);
}} else if (input.startsWith("drop")){
contents=input.substring(6);
if(player.moveTo(contents))
System.out.println("you dropped " +contents);
else
System.out.println("You don't have "+ contents);
} else if(input.contains("status")){
System.out.println(player);}
else{
System.out.println("What?");}
}
}
}
公共静态无效播放(播放器){
Scanner localScanner=新的扫描仪(System.in);
while(localScanner.hasNextLine()){
字符串输入=localScanner.nextLine();
if(input.equals(“quit”)){
System.out.println(“游戏结束,再见”);
回来
}
if(input.contains(“北”)){
buildWorld(player,player.moveTo().connectNorth());
}else if(input.contains(“南”)){
buildWorld(player,player.moveTo().connectSouth());
}else if(input.contains(“东”)){
buildWorld(player,player.moveTo().connectEast());
}else if(input.contains(“west”)){
buildWorld(player,player.moveTo().connectWest());
}否则{
字符串内容;
如果(!contents.equals(“”)
System.out.println(“有:\n”+内容);
否则{
System.out.println(“这个房间是空的。”);
}否则如果(input.startsWith(“pickup”){/这部分就是问题所在:
if (!contents.equals(""))
System.out.println("There is:\n" + contents);
else{
System.out.println("This room is empty.");}
else if(input.startsWith("pickup")) {
// ...
}
在else
之后不能有else if
我想问题是在else
之后有一个else if
。尝试将else
移动到该序列的最后一个
此外,在相同的if
后面还有两个else
语句:
else {
String contents;
...
}
...
else{
System.out.println("What?");}
这将导致额外的问题,甚至毫无意义。使用标准代码格式将有所帮助。你的缩进和括号放置是如此随机,我们无法在不阅读所有内容的情况下确定…你就是不能做一个else
-else if
标准else基本上是if-else链中的全部。哟else是最后通牒,如果交换它们,就不能再使用else。大多数IDE甚至不允许您尝试编译语法错误类型的代码…我建议使用Eclipse作为IDE