如何验证范围内的用户输入?和For Loop问题/混淆



如何验证用户输入的年份,范围必须在1900-2021之间。我也对在我的情况下使用for循环感到困惑。我需要打印出摩托车加速四次,然后在我需要代码后打印出制动/减速三次。我有一个java类和一个java主类。我尝试实现一个单独的方法来验证年份,但最终导致程序崩溃。

public class Motorcycle {
private int year;
private String make;
private int speed;
/**
* 
* @param year
* @param make 
*/
public Motorcycle(int year, String make) {
this.year = year;
this.make = make;
this.speed = 0;
}
public Motorcycle(){
this.year = 0;
this.make = "";
this.speed = 0;
}
public void setYear(int year) {
this.year = year;
}
public void setMake(String make) {
this.make = make;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getYear() {
return year;
}
public String getMake() {
return make;
}
public int getSpeed() {
return speed;
}
public void accelerate() {
this.speed += 5;
}
public void brake() {
this.speed -= 5;
}
@Override
public String toString() {
return "A " + year +" " + make + " going "+ speed +" miles per hour";

import java.util.*;

公共类摩托车Demo{

public static void main(String[] args) {
int year;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the year of your Motorcycle");  //validate year 1900 -2021 
year = keyboard.nextInt();
System.out.println("Enter the make of your Motorcycle(such as, Harley, Kawasaki)");
String maker = keyboard.next();
Motorcycle motor1 = new Motorcycle(year, maker);
System.out.println(motor1); //account for 0 speed toString()
for (int i = 0; i <= 3; i++) {            //use for loop  //accelerate 4 times and brake 3 times 
motor1.accelerate();
System.out.println(motor1);
//System.out.println("A " + year + " " + maker + " going " + motor1.getSpeed() + "miles per hour.");
//to String method     
}
for (int i = 0; i <= 3; i++);
{
motor1.brake();
System.out.println(motor1);
}
}

}

如果摩托车加速4次并制动3次,则输出应显示:";年份品牌(摩托车品牌(的速度为英里/小时。以0 mph的速度开始,达到20的最大速度,然后减速4次,得出速度为5 mph的结论。

如有任何帮助,我们将不胜感激并提前表示感谢。:(

然后尝试围绕输入处理设置while/do-while循环。如果输入不正确,通过将布尔值设置为与while条件相同来保持循环运行,如果答案是正确的,则中断循环

public static void main(String[] args) {
int year;
int currentYear = Calendar.getInstance().get(Calendar.YEAR); //Creating current year
boolean looping = false;
do {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the year of your Motorcycle");  //validate year 1900 -2021
year = keyboard.nextInt();
if (year < 1900 || year > currentYear) {
looping = true;
System.out.println("Invalid year!nPlease type in valid year!");
} else
System.out.println("Enter the make of your Motorcycle(such as, Harley, Kawasaki)");
String maker = keyboard.next();
//Put condition here too if necessary, and put looping true if input is bad
} while(looping);  //As long as looping is set true it reruns form
Motorcycle motor1 = new Motorcycle(year, maker);
System.out.println(motor1); //account for 0 speed toString()
for (int i = 0; i <= 3; i++) {            //use for loop  //accelerate 4 times and brake 3 times
motor1.accelerate();
System.out.println(motor1);
//System.out.println("A " + year + " " + maker + " going " + motor1.getSpeed() + "miles per hour.");
//to String method
}
for (int i = 0; i <= 3; i++);
{
motor1.brake();
System.out.println(motor1);
}
}

相关内容

  • 没有找到相关文章

最新更新