【发布时间】:2019-04-03 04:08:48
【问题描述】:
我对该项目的说明如下:
说明:使用哨兵值循环。创建一个基本的租车计算器
询问每个用户:
车辆类型(可以使用字符串以外的其他内容,例如:1 代表经济型,2 代表轿车等) 租用天数 计算(针对每个客户):
租金, 税收, 到期总额。 有三种不同的租金选择:经济 @ 31.76、轿车 @ 40.32、SUV @ 47.56。 [注意:只考虑全天单位(无小时费率)]。
销售税为 TOTAL 的 6%。
使用以下方法创建摘要数据:
客户数量 总收钱。 此外,还包括 IPO、算法和案头检查值(设计文档)。
{我要做什么和我的问题}
package tests;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Tester {
public static void main(String []args){
int count=0;
int days;
int cus = 10;
double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
boolean F1 = false, F2 = false, F3 = false;
Scanner in=new Scanner(System.in);
while (F3 == false) {
F3 = true;
System.out.print("Press 1 to enter Rental Calculator or else press 0 to quit\n");
System.out.println("Please only enter 1 or 0. Also, please only enter number(s) not letter(s)");
try {
cus=in.nextInt();
if (cus == 0 || cus == 1) {
F3 = true;
} else {
F3 = false;
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
F3 = false;
System.out.println("Invalid entry");
in.next();
}
}
if(cus == 1) {
while(F1 == false) {
F1 = true;
count++;
System.out.print("What vehical would you like to rent?\n");
System.out.println("Enter 1 for an economy car");
System.out.println("Enter 2 for a sedan car");
System.out.println("Enter 3 for an SUV");
//
try {
CarType = in.nextInt();
if (CarType <= 0 || CarType >= 4) {
System.out.print("Number must be 1-3\n");
System.out.println("Please enter 1 for an economy car");
System.out.println("Enter 2 for a sedan car");
System.out.println("Enter 3 for an SUV");
F1 = false;
} else {
if (CarType == 1) {
F1 = true;
DailyFee=31.76;
} else if(CarType == 2) {
F1 = true;
DailyFee=40.32;
} else if(CarType == 3) {
F1 = true;
DailyFee=47.56;
}
while (F2 == false) {
F2 = true;
try {
System.out.print("Please enter the number of days rented. (Example; 3) : ");
days = in.nextInt();
if (days <= 0) {
System.out.println("Number of days must be more than zero");
F2 = false;
} else {
double x=days;
NontaxTotal = (DailyFee * x);
Total = (NontaxTotal * 1.06);
FullTotal+=Total;
F3 = true;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
F2 = false;
in.next();
}
}
}
} catch (InputMismatchException ex) {
F1 = false;
System.out.println("Answer must be a number");
}
}
}
in.close();
System.out.println("Count of customers : " + count);
System.out.printf("Total of the Day : $ %.2f", FullTotal);
}
}
{我的问题}
当在提示“按 1 进入租金计算器或按 0 退出”提示输入字母时,会显示错误提示,然后控制台要求再次输入。类似地,当在提示“您想租什么车辆?”时输入一个字母时。控制台继续不停地打印行?我不知道如何解决这个问题?
我希望我的程序允许进行多个计算输入。但是,在完整的计算输入(天 * 税 * 汽车类型)之后,控制台发布汇总数据而不是循环? 2a.在提示“请输入租用的天数。(示例;3):”后,并跟随用户输入。我如何让我的程序循环回到询问“按 1 进入租金计算器或按 0 退出”?仍然使 0 提示我的摘要数据?
【问题讨论】:
-
什么是 F1、F2 和 F3(逻辑上)?它们有什么价值?你提供什么输入? .... ?
-
@Stultuske 我确实编辑了一些问题,希望对您有所帮助? F1、F2 和 F3 就像循环的检查点? (我是新来的)它们除了真/假之外没有任何价值。 true 允许循环向前移动, false 导致循环在未满足检查点时重置。比如不输入1或0等...
-
@Stultuske 我需要用户输入的唯一信息是 1. 车型,2. 租赁长度,以及 3. 是否需要进行多次租赁计算?汇总数据需要有 1. Total of all car(s) 2. total of all car(s) [day *tax *cartype]
-
您所说的从业务角度来说,它们的值要么是真要么是假,但在逻辑上:shouldContinue、dataReadSuccessFully、errorOccured 是它们可能的意思是合乎逻辑的,并且更容易阅读代码。是的,您可能只需要那个输入,但您在运行代码时提供的输入是什么?
-
@Stultuske 我有点困惑不会说谎,但是我输入的输入后面跟着一个 = 符号,它是:“”按 1 进入租金计算器,否则按 0 退出“” = 1 [NEXT] “你想租什么车?” = one(这是我遇到的第一个错误和问题)
标签: java loops if-statement type-mismatch inputmismatchexception