學習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
謝謝,克里斯。你真棒。 –