gpt4 book ai didi

java - Java Scanner 输入和 while 循环出现问题,其中包含多个 if 语句

转载 作者:行者123 更新时间:2023-12-02 09:12:52 25 4
gpt4 key购买 nike

我希望使用 while 循环限制整数值的输入,在某些值之间,并在其中包含几个 if-else if-else 语句!它有点工作,但不完全像它应该的那样......也想到使用开关,但我太“绿色”不知道如何!如果有人愿意并且知道如何...我也欢迎使用开关!如果需要的话,甚至可以使用嵌套开关......这是我的代码:

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);
//System.out.println("Engine is on!");
System.out.println("Do you wish to depart? Shift in to the first gear then and accelerate!");
System.out.println("Type in the speed: ");
Honda.accelerate(sc);
System.out.println("We are departing! Shifting in to " + Honda.currentGear +
"st gear and accelerating to " + Honda.currentSpeed + " km per hour!");
}

构造函数和函数:

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) {

while(in.hasNext()) {
String input = in.nextLine();
if(input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) {
isEngineOn = true;
System.out.println("Engine is on!");
return input;
} else {
System.out.println("Your input is not correct! Please start the engine!");
}
}
return null;
}

public int accelerate(Scanner in){
while(in.hasNextInt()){
currentSpeed = in.nextInt();
if(isEngineOn && currentSpeed > 0){
currentGear++;
} else if(currentSpeed > 50){
System.out.println("We cannot accelerate to more than 50 km per hour, when shifting in the 1st gear!");
} else{
System.out.println("We cannot depart at 0 km per hour!");
}
}
return 0;
}
}

它正在接受输入,但没有按照应有的方式进一步处理,也没有给出错误消息或停止应用程序,我的错误是什么?

最佳答案

更改 if 语句的顺序将会起作用。

在您当前的方法中:

if(isEngineOn && currentSpeed > 0)

对于您输入的任何值,始终返回 true。

使用这种方法会让你更进一步,虽然我怀疑它仍然不会是你所期望的,但我希望它能帮助你朝着正确的方向前进。

public int accelerate(Scanner in){
while(in.hasNextInt()){
currentSpeed = in.nextInt();
if(currentSpeed > 50 && currentGear <= 1){
System.out.println("We cannot accelerate to more than 50 km per hour, when shifting in the 1st gear!");
} else if(isEngineOn && currentSpeed > 0){
currentGear++;
break; /* I've added this to break out of the method to progress in your flow */
} else{
System.out.println("We cannot depart at 0 km per hour!");
}
}
return 0;
}
}

关于java - Java Scanner 输入和 while 循环出现问题,其中包含多个 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com