package somePackage;
import java.util.Scanner;
public class SomeClass {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.print("Please enter a command (start or stop) : ");
String scanner = input.nextLine();
if ("start".equals(scanner)) {
System.out.println("System is starting");
} else if ("stop".equals(scanner)) {
System.out.println("System is closing");
}
while (!"start".equals(scanner) && (!"stop".equals(scanner))) {
System.out.print("Please try again : ");
scanner = input.nextLine();
}
}
}
當用戶沒有輸入「開始」或「停止」。該程序將要求用戶「再試一次:」。假設用戶在此之後輸入「開始」,則輸出將爲空白。我如何讓循環返回到if()或else if()語句中的原始System.out.print()?如何讓我的while()循環返回if()語句?
P.S,我是新來的Java所以任何反饋將幫助:)謝謝!
if語句必須位於while循環中。 –
你能給我一個例子嗎? – Crypto
請注意,從'Scanner'中讀取的稱爲'scanner'的''''''''''''很容易混淆。考慮交換這些名字。 –