对不起,如果这最终成为一个多余的问题,我是java的新手,并且无法找到答案,但这可能是因为我不确定100%如何表达我的问题。
无论如何,这是我为练习而编写的一些代码
import java.io.*;
import java.util.Scanner;
class Airplane {
int topSpeed;
int serviceCeling;
int weight;
int wingspan;
public Airplane(int maxSpeed, int maxAltitude, int totalWeight, int widthOfWIngs){
topSpeed = maxSpeed;
serviceCeling = maxAltitude;
weight = totalWeight;
wingspan = widthOfWIngs;
}
public void setSpeed(){
int speed;
Scanner speedCheck = new Scanner(System.in);
System.out.println("Input new desired speed:");
speed = speedCheck.nextInt();
if (speed > topSpeed) {
System.out.println("Requested speed exceeds aircrafts capabilities");
} else if (speed <= 100) {
System.out.println("Requested speed is too slow to maintain flight");
} else {
System.out.println("You are now travelling " + speed + "kph");
}
}
public static void main(String[] args){
Airplane boeing = new Airplane(750,30000,45,75);
boeing.setSpeed();
}
}
我遇到的问题是setSpeed方法中的if语句。现在,如果我输入的值太高或太低,我都会收到正确的错误消息,但是程序只会退出。我希望它重新提示用户,直到给出有效值为止,但是我不确定如何去做。
只要能提供任何帮助,我都会道歉,如果无法解决这个问题,我很抱歉,我无法在搜索中很好地描述它来获得答案。