[英]How can I restrict the "new Scanner(System.in)" system in Java, to not go further with the user input data, unless it's the correct inputted value?
我正在學習 Java,並且我的知識有所提高。 我希望應用程序根據用戶輸入的值走得更遠,前提是它是正確的,否則我希望相同的問題(控制台輸入)自行重復(不使用應用程序走得更遠),直到用戶輸入正確的價值! 我的代碼如下所示:
package oop.project;
import java.util.Scanner;
/**
*
* @author Dragos
*/
public class OOPProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
Car Honda = new Car(2018, 20000, "Honda", "Civic", 200, 6, 0, 0);
System.out.println("Manufacturer is: " + Honda.maker + ", model: " + Honda.model +
", year of fabrication: " + Honda.year + ", price: " + Honda.price +
"!");
System.out.println("Please start the engine of your vehicle, by typing in 'Yes' or 'Start'
or 'Turn on'!");
System.out.print("Do you wish to start the engine?");
System.out.println(" ");
Honda.StartEngine(sc);
}
構造函數類及其功能:
public class Car {
public int year;
public int price;
public String maker;
public String model;
public int maximumSpeed;
public int numberOfGears;
public int currentSpeed;
public int currentGear;
public boolean isEngineOn;
public Car(int year, int price, String maker, String model, int maximumSpeed,
int numberOfGears, int currentSpeed, int currentGear) {
this.year = year;
this.price = price;
this.maker = maker;
this.model = model;
this.maximumSpeed = maximumSpeed;
this.numberOfGears = numberOfGears;
this.currentSpeed = currentSpeed;
this.currentGear = currentGear;
}
public String StartEngine(Scanner in) {
String input = in.nextLine();
do{
isEngineOn = true;
} while(input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));
if(isEngineOn) {
System.out.println("Engine is on!");
} else {
System.out.println("Your input is not correct! Please start the engine!");
isEngineOn = false;
}
return input;
}
在 do & while 之前,我只有 if-else 語句與(input.equals("Yes") || input.equals("Start") || input.equals("Turn on"))
在 if語句,但不管用戶在 IDE 控制台中輸入了錯誤的單詞這一事實,該應用程序無論如何都會繼續前進,當輸入錯誤的單詞時,它確實會顯示正確的消息,但不會停止或限制用戶輸入正確的值! 試圖通過添加 do-while 語句來限制用戶輸入正確的值,但效果不佳。
在獲得有效輸入之前,您必須處於循環狀態。 試試下面的片段。
while(in.hasNext()) {
String input = in.nextLine();
if(input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) {
System.out.println("Engine is on!");
break;
} else {
System.out.println("Your input is not correct! Please start the engine!");
}
}
如果你想從函數中返回輸入,你可以像下面那樣做。
while (in.hasNext()) {
String input = in.nextLine();
if (input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) {
System.out.println("Engine is on!");
return input;
} else {
System.out.println("Your input is not correct! Please start the engine!");
}
}
return null;
while(!isEngineOn) {
String input = in.nextLine();
isEngineOn = input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));
if (!isEngineOn) System.out.println("Your input is not correct! Please start the engine!");
}
或者像這樣,如果你以后需要答案:
String input;
while(!isEngineOn) {
input = in.nextLine();
isEngineOn = input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));
if (!isEngineOn) System.out.println("Your input is not correct! Please start the engine!");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.