学习Java,现在我不知道为什么,但是这段代码一直给我提供else if
语句的问题。为什么我在Java中的代码给我带来了如此多的麻烦,如果在Jgrasp这个特定的代码行中有else语句?
public class Sherlock
{
public static void main(String[] args)
{
String answer = "Watson";
String response = "";
int tries = 0;
Scanner input = new Scanner(System.in);
while (tries <=3);
{
System.out.print("Enter the name of Sherlock's partner, and dear friend. ");
response = input.nextLine();
tries++;
if (response.equals ("Watson"))
while (tries<= 3)
{
System.out.println("Yes, that is right, Barrel Rider.");
break;
}
else if (tries == 3)
{
System.out.println("Ooooh, sorry kid but it looks like you are S.O.L.");
break;
}
else
while (tries <= 3)
{
System.out.println("Sorry, try again!");
}
}
}
}
的,如果else语句错误已经或多或少地得到解决,但现在我得到一个不同的错误:
Sherlock.java:24:错误:打破外界开关或循环 休息; ^ 1错误
为什么它会一直坚持我把开关或回路放在外面?
我已经格式化了您的代码。正确格式的代码是编写无错代码的第一步。它使得找到不应该存在的东西变得更容易...就像在'while循环语句和你的第一个'if语句之后的错误分号一样。 –
好眼福尔摩;) – JFPicard
谢谢,克里斯。你真棒。 –