问:不了解循环过程吗?或如果可能的话?

时间:2018-10-29 18:50:06

标签: java loops if-statement while-loop double

我正在从事一个涉及创建出租车计算器的项目。

我要做的是在被问到“您想租什么车?”的情况下放到哪里。如果在提示用户时输入的数字不在1-3之间,那么我希望程序再次循环到被询问的车辆类型。

类似地,当提示您输入'请输入租用天数。 (示例; 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);
    }   
}

1 个答案:

答案 0 :(得分:0)

让我帮您解决这个问题, 我为您编写了此代码,我尝试了一下并成功了 这将检查两个答案是否均为整数(整数)且大于零,并且还将首先检查答案是否为数字,以便用户用字母回答时,将提示他再试一次 这是我的建议: 基本上,我将try-catch块与InputMismatchException一起使用来检测答案是否不是整数(整数)或根本不是数字,每次检测到错误时,我都会将布尔值翻转为false并保持循环,只要此布尔值是错误的(我在检查之前将布尔值改回true,否则一旦用户输入错误的答案,即使之后他给出了正确的答案,也会始终提示他回答)

    int vehicleType;
    int numberOfDays;
    double dailyFee;
    boolean validAnswer1 = false;
    boolean validAnswer2 = false;

    Scanner scan = new Scanner(System.in);

    while (validAnswer1 == false) {
        validAnswer1 = true;
        System.out.println("Choose Vehicle Type");
        try {
            vehicleType = scan.nextInt();
            if (vehicleType <= 0) {
                System.out.println("Number must be more than zero");
                validAnswer1 = false;
            } else if (vehicleType >= 4) {
                System.out.println("Number should be from 1 to 3");
                validAnswer1 = false;
            } else {
                if (vehicleType == 1) {
                    dailyFee=31.76;
                } else if(vehicleType == 2) {
                    dailyFee=40.32;
                }else if(vehicleType == 3) {
                    dailyFee=47.56;
                }
                while (validAnswer2 == false) {
                    validAnswer2 = true;
                    try {
                        System.out.println("Enter number of days rented ?");
                        numberOfDays = scan.nextInt();
                        if (numberOfDays <= 0) {
                            System.out.println("Number of days must be more than zero");
                            validAnswer2 = false;
                        } else {
                            // calculate your rent total here
                        }
                    } catch(InputMismatchException ex) {
                        System.out.println("Answer must be an Integer");
                        validAnswer2 = false;
                        scan.next();
                    }
                }                   
            }
        } catch (InputMismatchException ex) {
            validAnswer1 = false;
            System.out.println("Answer must be an Integer");     
            scan.next();
        }           
    }

希望这很有用,如果您仍然需要帮助,请告诉我