猿问

有几个问题请教,谢谢!!???

在输入1或者2可以执行了,那如果输入3,就是指令有误,重新输入怎么写?输入其他抛出异常后,再次重新输入呢?还有如果后面选择2取消,返回到System.out.println("-----下面是您可选择的车型和价格表-----");这里,怎么写?


package imooc;


import java.util.InputMismatchException;

import java.util.Scanner;


public class Leased {

public static void main(String[] args) {

System.out.println("欢迎使用哒哒租车");

Scanner sc = new Scanner(System.in);

System.out.println("是否租车:1.租车2.不租车");

try{

int input = sc.nextInt();

if (input==1) {

System.out.println("-----下面是您可选择的车型和价格表-----");

System.out.println("序号\t汽车名称\t租金\t载客量\t载货量");

Car[] carsForRent = { new Minibus("奥迪A4", 500, 4),

new Mazda("马自达6", 400, 4), new Pickup("皮卡雪6", 450, 4, 2),

new Jinlong("金龙", 800, 20), new Songhua("松花江", 400, 4),

new Ivecoa("依维柯", 1000, 20) };

for (Car currentCar : carsForRent) {

if (currentCar instanceof Minibus) {

System.out.println("" + 1 + "\t" + currentCar.getModel()

+ "" + "\t" + currentCar.getRent() + "元/天" + "\t"

+ currentCar.getCapacity() + "人");


}

if (currentCar instanceof Mazda) {

System.out.println("" + 2 + "\t" + currentCar.getModel()

+ "" + "\t" + currentCar.getRent() + "元/天" + "\t"

+ currentCar.getCapacity() + "人");


}

if (currentCar instanceof Pickup) {

System.out.println("" + 3 + "\t" + currentCar.getModel()

+ "" + "\t" + currentCar.getRent() + "元/天" + "\t"

+ currentCar.getCapacity() + "人" + "\t"

+ currentCar.getCargo() + "吨");


}

if (currentCar instanceof Jinlong) {

System.out.println("" + 4 + "\t" + currentCar.getModel()

+ "" + "\t" + currentCar.getRent() + "元/天" + "\t"

+ currentCar.getCapacity() + "人");


}

if (currentCar instanceof Songhua) {

System.out.println("" + 5 + "\t" + currentCar.getModel()

+ "" + "\t" + currentCar.getRent() + "元/天" + "\t"

+ "" + "\t" + currentCar.getCargo() + "吨");


}

if (currentCar instanceof Ivecoa) {

System.out.println("" + 6 + "\t" + currentCar.getModel()

+ "" + "\t" + currentCar.getRent() + "元/天" + "\t"

+ "" + "\t" + currentCar.getCargo() + "吨");


}

}


System.out.println("请输入你所选择车型所对应的序号");

String input1 = sc.next();

switch (input1) {

case "1":

System.out.println("你所选择的车型是:" + carsForRent[0].getModel() + ""

+ "每日租金为:" + carsForRent[0].getRent() + "元" + "" + "载客量:"

+ carsForRent[0].getCapacity() + "人");

break;

case "2":

System.out.println("你所选择的车型是:" + carsForRent[1].getModel() + ""

+ "每日租金为:" + carsForRent[1].getRent() + "元" + "" + "载客量:"

+ carsForRent[1].getCapacity() + "人");

break;

case "3":

System.out.println("你所选择的车型是:" + carsForRent[2].getModel() + ""

+ "每日租金为:" + carsForRent[2].getRent() + "元" + "" + "载客量:"

+ carsForRent[2].getCapacity() + "人" + "" + "载货量:"

+ carsForRent[2].getCargo() + "吨");

break;

case "4":

System.out.println("你所选择的车型是:" + carsForRent[3].getModel() + ""

+ "每日租金为:" + carsForRent[3].getRent() + "元" + "" + "载客量:"

+ carsForRent[3].getCapacity() + "人");

break;

case "5":

System.out.println("你所选择的车型是:" + carsForRent[4].getModel() + ""

+ "每日租金为:" + carsForRent[4].getRent() + "元" + "" + "载货量:"

+ carsForRent[4].getCargo() + "吨");

break;

case "6":

System.out.println("你所选择的车型是:" + carsForRent[5].getModel() + ""

+ "每日租金为:" + carsForRent[5].getRent() + "元" + "" + "载货量:"

+ carsForRent[5].getCargo() + "吨");

break;

}

System.out.println("请输入您要出租的天数");

int i = sc.nextInt();

if (i > 0 || i < 365) {

System.out.println("您要出租" + i + "天");


}

System.out.println("是否确认:1.确认2.取消");

String input2 = sc.next();

if (input2.equals("1")) {

System.out.println("租车成功,欢迎再次光临!");

} else {

System.out.println("租车失败,谢谢使用哒哒租车!");

}


}else if(input==2){

System.out.println("谢谢使用哒哒租车,再见");


}else{

System.out.println("您输入的指令有误,请重新输入");

}

}catch(InputMismatchException e){

System.out.println("指令有误,您只能输入1或者2");

}

}

}


慕粉3301778
浏览 1172回答 2
2回答

快过来让我看看

把输入参数放在一个while循环里面不就可以一直接受输入了吗

whereismybible

兄弟你输入完就没了。要控制循环条件的。
随时随地看视频慕课网APP

相关分类

Java
我要回答