问:执行多个循环和多个 if 语句和 if-else 语句 ||租车计算器项目
Q : Doing multiple loops and multiple if-statements and if-else-statements || RENTAL CAR CALCULATOR PROJECT
我对该项目的指示如下:
说明:使用标记值循环。创建基本的租车计算器
向每个用户询问:
车辆类型(可以使用字符串以外的其他内容,例如:1 表示经济型,2 表示轿车等)
租用天数
计算(对于每个客户):
租金,
税收,
总到期。
共有三种不同的租赁选项,价格各不相同:经济型 @ 31.76,轿车 @ 40.32,SUV @ 47.56。 [注意:仅考虑全天单位(无小时费率)]。
销售税为总额的 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);
}
}
{我的问题}
当输入一个字母到它显示的提示"Press 1 to enter Rental Calculator or else press 0 to quit"时,一个错误提示然后控制台再次要求输入。同理,在"What vehicle would you like to rent?"提示符下输入一个字母,控制台继续不停地打印行?我不知道如何解决这个问题?
我希望我的程序允许进行多个计算输入。但是,在完全计算输入(天数 * 税收 * 车型)后,控制台 post 汇总数据而不是循环?
2a.在提示 "Please enter the number of days rented. (Example; 3) : " 和用户输入之后。我如何让我的程序循环回到询问 "Press 1 to enter Rental Calculator or else press 0 to quit"?仍然使 0 提示我的汇总数据?
我刚刚 "refactored" 你的代码,删除了一些过时的代码并将一些其他代码放在其他位置。
我还为变量使用了更清晰的命名,并遵循了命名约定。
你遇到的问题是,你没有在每个 catch block
中有一个 in.next();
意味着在迭代时,变量一直使用相同的变量(这是无效的)所以一直循环错误信息。
现在这段代码远非完美,仍然可以轻松改进,但这应该可以帮助您入门。
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;
int carType;
double dailyFee=0, nonTaxTotal, total,fullTotal=0;
boolean checkRunOrQuit = false, chooseTypeVehicle = false, numberOfDAysChosen = false;
Scanner in=new Scanner(System.in);
while ( !checkRunOrQuit ) {
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();
switch ( cus ) {
case 0: System.out.println("End of application");
System.exit(0); // This will actually end your application if the user enters 0, no need to verify later on
break;
case 1: checkRunOrQuit = true;
break;
default:
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
System.out.println("Invalid entry: ");
in.next();
}
}
while( !chooseTypeVehicle ) { // --> simplified comparison
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();
chooseTypeVehicle = true;
switch ( carType ) {
case 1: dailyFee = 31.76;
break;
case 2: dailyFee = 40.32;
break;
case 3: dailyFee = 47.56;
break;
default:
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");
chooseTypeVehicle = false;
break;
}
} catch (InputMismatchException ex) {
System.out.println("Answer must be a number");
in.next(); // -> you forgot this one.
}
}
while ( !numberOfDAysChosen ) {
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");
} else {
nonTaxTotal = (dailyFee * days);
total = (nonTaxTotal * 1.06);
fullTotal+=total;
numberOfDAysChosen = true;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
in.next();
}
}
in.close();
System.out.println("Count of customers : " + count);
System.out.printf("total of the Day : $ %.2f", fullTotal);
}
}
给你,我稍微修改了一下,把整个东西放在一个 while 循环中 (while (cus != 0)) 现在它运行良好,试试这个代码,如果你有问题请告诉我
package inter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Inter {
public static void main(String []args){
int count=0;
int days;
int cus = 10; // added
double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
boolean F1 = false, F2 = false;
Scanner in=new Scanner(System.in);
while (cus != 0) {
while (true) {
System.out.println("If there are any customer press 1 else press 0");
try {
cus=in.nextInt();
if (cus == 0 || cus == 1) {
break;
} else {
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
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 {
//days = in.nextInt();
double x=days;
NontaxTotal = (DailyFee * x);
Total = (NontaxTotal * 1.06);
FullTotal+=Total;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
F2 = false;
in.next();
}
}
F2 = false;
}
} catch (InputMismatchException ex) {
F1 = false;
System.out.println("Answer must be a number");
in.next();
}
}
F1 = false;
}
}
System.out.println("Count of customers : " + count);
System.out.printf("Total of the Day : $ %.2f", FullTotal);
}
}
我对该项目的指示如下:
说明:使用标记值循环。创建基本的租车计算器
向每个用户询问:
车辆类型(可以使用字符串以外的其他内容,例如:1 表示经济型,2 表示轿车等) 租用天数 计算(对于每个客户):
租金, 税收, 总到期。 共有三种不同的租赁选项,价格各不相同:经济型 @ 31.76,轿车 @ 40.32,SUV @ 47.56。 [注意:仅考虑全天单位(无小时费率)]。
销售税为总额的 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);
}
}
{我的问题}
当输入一个字母到它显示的提示"Press 1 to enter Rental Calculator or else press 0 to quit"时,一个错误提示然后控制台再次要求输入。同理,在"What vehicle would you like to rent?"提示符下输入一个字母,控制台继续不停地打印行?我不知道如何解决这个问题?
我希望我的程序允许进行多个计算输入。但是,在完全计算输入(天数 * 税收 * 车型)后,控制台 post 汇总数据而不是循环? 2a.在提示 "Please enter the number of days rented. (Example; 3) : " 和用户输入之后。我如何让我的程序循环回到询问 "Press 1 to enter Rental Calculator or else press 0 to quit"?仍然使 0 提示我的汇总数据?
我刚刚 "refactored" 你的代码,删除了一些过时的代码并将一些其他代码放在其他位置。 我还为变量使用了更清晰的命名,并遵循了命名约定。
你遇到的问题是,你没有在每个 catch block
中有一个 in.next();
意味着在迭代时,变量一直使用相同的变量(这是无效的)所以一直循环错误信息。
现在这段代码远非完美,仍然可以轻松改进,但这应该可以帮助您入门。
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;
int carType;
double dailyFee=0, nonTaxTotal, total,fullTotal=0;
boolean checkRunOrQuit = false, chooseTypeVehicle = false, numberOfDAysChosen = false;
Scanner in=new Scanner(System.in);
while ( !checkRunOrQuit ) {
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();
switch ( cus ) {
case 0: System.out.println("End of application");
System.exit(0); // This will actually end your application if the user enters 0, no need to verify later on
break;
case 1: checkRunOrQuit = true;
break;
default:
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
System.out.println("Invalid entry: ");
in.next();
}
}
while( !chooseTypeVehicle ) { // --> simplified comparison
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();
chooseTypeVehicle = true;
switch ( carType ) {
case 1: dailyFee = 31.76;
break;
case 2: dailyFee = 40.32;
break;
case 3: dailyFee = 47.56;
break;
default:
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");
chooseTypeVehicle = false;
break;
}
} catch (InputMismatchException ex) {
System.out.println("Answer must be a number");
in.next(); // -> you forgot this one.
}
}
while ( !numberOfDAysChosen ) {
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");
} else {
nonTaxTotal = (dailyFee * days);
total = (nonTaxTotal * 1.06);
fullTotal+=total;
numberOfDAysChosen = true;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
in.next();
}
}
in.close();
System.out.println("Count of customers : " + count);
System.out.printf("total of the Day : $ %.2f", fullTotal);
}
}
给你,我稍微修改了一下,把整个东西放在一个 while 循环中 (while (cus != 0)) 现在它运行良好,试试这个代码,如果你有问题请告诉我
package inter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Inter {
public static void main(String []args){
int count=0;
int days;
int cus = 10; // added
double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
boolean F1 = false, F2 = false;
Scanner in=new Scanner(System.in);
while (cus != 0) {
while (true) {
System.out.println("If there are any customer press 1 else press 0");
try {
cus=in.nextInt();
if (cus == 0 || cus == 1) {
break;
} else {
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
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 {
//days = in.nextInt();
double x=days;
NontaxTotal = (DailyFee * x);
Total = (NontaxTotal * 1.06);
FullTotal+=Total;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
F2 = false;
in.next();
}
}
F2 = false;
}
} catch (InputMismatchException ex) {
F1 = false;
System.out.println("Answer must be a number");
in.next();
}
}
F1 = false;
}
}
System.out.println("Count of customers : " + count);
System.out.printf("Total of the Day : $ %.2f", FullTotal);
}
}