Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Q:执行多个循环、多个if语句和if else语句| |租车计算器项目_Java_Loops_If Statement_Type Mismatch_Inputmismatchexception - Fatal编程技术网

Java Q:执行多个循环、多个if语句和if else语句| |租车计算器项目

Java Q:执行多个循环、多个if语句和if else语句| |租车计算器项目,java,loops,if-statement,type-mismatch,inputmismatchexception,Java,Loops,If Statement,Type Mismatch,Inputmismatchexception,我对该项目的指示如下: 说明:使用sentinel值循环。创建基本租车计算器的步骤 向每个用户询问: 车辆类型(可能使用字符串以外的内容,例如:1表示经济型,2表示轿车等) 租用天数 计算(每个客户的)成本: 租金, 税, 应付总额。 有三种不同的租金选择,价格不同:经济型31.76英镑,轿车40.32英镑,SUV 47.56英镑。[注:仅考虑全天单位(无小时费率)] 销售税=总额的6% 使用以下内容创建摘要数据: 顾客人数 收集的总金额。 此外,还包括IPO、算法和桌面检查值(设计文档) {我

我对该项目的指示如下:

说明:使用sentinel值循环。创建基本租车计算器的步骤

向每个用户询问:

车辆类型(可能使用字符串以外的内容,例如:1表示经济型,2表示轿车等) 租用天数 计算(每个客户的)成本:

租金, 税, 应付总额。 有三种不同的租金选择,价格不同:经济型31.76英镑,轿车40.32英镑,SUV 47.56英镑。[注:仅考虑全天单位(无小时费率)]

销售税=总额的6%

使用以下内容创建摘要数据:

顾客人数 收集的总金额。 此外,还包括IPO、算法和桌面检查值(设计文档)

{我要做的事情和我的问题}

包测试;
导入java.util.InputMismatchException;
导入java.util.Scanner;
公共类测试员{
公共静态void main(字符串[]args){
整数计数=0;
国际日;
int-cus=10;
double DailyFee=0,NontaxTotal,CarType,Total,FullTotal=0;
布尔值F1=false,F2=false,F3=false;
扫描仪输入=新扫描仪(系统输入);
while(F3==false){
F3=真;
System.out.print(“按1进入租金计算器或按0退出”);
System.out.println(“请只输入1或0。另外,请只输入数字而不是字母”);
试一试{
cus=in.nextInt();
如果(cus==0 | | cus==1){
F3=真;
}否则{
F3=假;
System.out.println(“数字必须为1或0”);
}
}捕获(输入不匹配异常){
F3=假;
System.out.println(“无效条目”);
in.next();
}
}
如果(cus==1){
while(F1==false){
F1=真;
计数++;
System.out.print(“您想租什么车?\n”);
System.out.println(“为经济型汽车输入1”);
System.out.println(“为轿车输入2”);
System.out.println(“为SUV输入3”);
// 
试一试{
CarType=in.nextInt();
如果(CarType=4){
系统输出打印(“编号必须为1-3\n”);
System.out.println(“经济型汽车请输入1”);
System.out.println(“为轿车输入2”);
System.out.println(“为SUV输入3”);
F1=假;
}否则{
if(CarType==1){
F1=真;
每日费用=31.76;
}else if(CarType==2){
F1=真;
每日费用=40.32;
}else if(CarType==3){
F1=真;
每日费用=47.56;
}
while(F2==false){
F2=真;
试试{
System.out.print(“请输入租用天数。(示例3):”;
天数=in.nextInt();
如果(天我刚刚“重构”了您的代码,删除了一些过时的代码,并在其他位置放置了一些其他代码。
我还对变量使用了更清晰的命名,并遵循命名约定

您遇到的问题是,您没有在每个
catch块中
都有一个
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);
    }
}
包测试;
导入java.util.InputMismatchException;
导入java.util.Scanner;
公共类测试员{
公共静态void main(字符串[]args){
整数计数=0;
国际日;
int cus;
int型;
双倍每日费用=0,非税合计,合计,全额合计=0;
布尔值checkRunOrQuit=false,chooseTypeVehicle=false,numberOfDAysChosen=false;
扫描仪输入=新扫描仪(系统输入);
而(!checkRunOrQuit){
System.out.print(“按1进入租金计算器或按0退出”);
System.out.println(“请只输入1或0。另外,请只输入数字而不是字母”);
试一试{
cus=in.nextInt();
交换机(cus){
案例0:System.out.println(“应用程序结束”);
System.exit(0);//如果用户输入0,这实际上将结束您的应用程序,以后无需验证
打破
案例1:checkRunOrQuit=true;
打破
违约:
System.out.println(“数字必须为1或0”);
}
}捕获(输入不匹配异常){
System.out.println(“无效条目:”);
in.next();
}
}
而(!chooseTypeVehicle){/-->简化了比较
计数++;
System.out.print(“您想租什么车?\n”);
System.out.println(“为经济型汽车输入1”);
System.out.println(“为轿车输入2”);
System.out.println(“为SUV输入3”);
试一试{
carType=in.nextInt();
选择TypeVehicle=true;
开关(carType){
案例1:每日费用=31.76;
打破
案例2:每日费用=40.32;
打破
案例3:每日费用=47.56;
打破
违约:
系统输出打印(“编号必须为1-3\n”);
System.out.println(“经济型汽车请输入1”);
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);
    }
}
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);
    }
}