[英]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.