【发布时间】:2018-10-29 18:50:05
【问题描述】:
我正在从事一个涉及创建租车计算器的项目。
当被问到:“你想租什么车??”时,我想做的是做到这一点。如果在提示用户时输入了一个不在 1-3 之间的数字,那么我希望程序循环回到再次询问车辆类型的位置。
同样,当提示“请输入租用天数”时。 (Example; 3) : ' 我只想让用户输入正整数。例如,不允许输入 3.1、2.35、0.35 -2 等...
这是我写的内容以及我对这些问题的尝试:
package inter;
import java.util.Scanner;
public class Inter {
public static void main(String []args){
int count=0;
int days;
double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
Scanner in=new Scanner(System.in);
System.out.println("If there are any customer press 1 else press 0");
int cus=in.nextInt();
while(cus!=0){
count++;
System.out.print("What vehical would you like to rent?\n");
System.out.println("Enter 1 for an economy car\n");
System.out.println("Enter 2 for a sedan car\n");
System.out.println("Enter 3 for an SUV");
CarType = in.nextInt();
if (CarType == 1) {
DailyFee=31.76;
}
else if(CarType == 2) {
DailyFee=40.32;
}
else if(CarType == 3) {
DailyFee=47.56;
}
else if(CarType <= 0) {
System.out.println("input is not a positive Integer ");
System.out.println("Please enter a positive integer value: ");
cus = 0; }
else if(CarType > 4) {
System.out.println("input is not a positive Integer ");
System.out.println("Please enter a positive integer value: ");
cus = 0; }
System.out.print("Please enter the number of days rented. (Example; 3) : ");
days = Integer.valueOf(in.nextLine());
double x=days;
NontaxTotal = (DailyFee * x);
Total = (NontaxTotal * 1.06);
FullTotal+=Total;
System.out.printf("The total amount due is $ %.2f \n",Total);
System.out.println("If there are any customer press 1 else press 0");
cus=in.nextInt();
}
System.out.println("Count of customers : "+count);
System.out.printf("Total of the Day : $ %.2f",FullTotal);
}
}
【问题讨论】:
-
do { //ask for and assign input; } while(CarType < 0 || CarType > 3); -
@GBlodgett 谢谢这解决了我的问题,即只允许输入 1-3 作为车辆类型。无论如何,你知道我在提示时如何只允许输入 1 或 0 的任何提示:“如果有任何客户按 1 否则按 0”,每当我尝试调整它时,我会弄乱我所拥有的循环如果输入 0,则汇总数据。?
标签: java loops if-statement while-loop double