当前位置: 首页 > 知识库问答 >
问题:

用开关循环?

武峻熙
2023-03-14

我想做的是创建一个租车计算器。这已经为三辆不同的汽车预装了每日费用和燃油费。我想在所有东西上加6%的销售税。用户的输入决定了总数等。。

完整项目详细信息:[https://www.scribd.com/document/392038034/Bob-s-Car-Rental-Intermediate-pdf]

{我的代码当前}

package tests;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Tester {

public static void main(String []args){
    int days, cus, carType, fuel, count=0;
    double dailyFee=0, nonTaxTotal=0, total=0, fullTotal=0, fuelcharge=0, taxes=0, avrg=0, Fullcharge=0;
    boolean checkRunOrQuit = false, chooseTypeVehicle = false, numberOfDAysChosen = false, fuellevel = false, reset=true;
    Scanner in=new Scanner(System.in);
while(!reset); 

    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.print("End of application\n");
                        System.out.println("Count of customers: " + count);
                        System.out.printf("Total fuel charges $ %.2f \n", Fullcharge);
                        System.out.printf("Total taxes collected: $ %.2f \n", taxes);
                        System.out.printf("Total of the Day: $ %.2f \n", fullTotal);
                        System.out.printf("The average bill was: $ %.2f \n", avrg);
                        System.exit(0);
                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 ) {

        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;
                count++;
                break;
                case 2: dailyFee = 40.32;
                count++;
                break;
                case 3: dailyFee = 47.56;
                count++;
                break;
                default:
                    System.out.print("Number must be 1-3\n");
                    chooseTypeVehicle = false;
                    break;
            }
        } catch (InputMismatchException ex) {
            System.out.println("Answer must be a number");
            in.next();
        }
    }

    while ( !fuellevel ) {
        try {
            System.out.print("Is the fuel empty?\n");
            System.out.println("Please enter 1 for yes.");
            System.out.println("Please enter 2 for no.");
            fuel = in.nextInt();
            if (fuel <= 0 |fuel > 2.1) {
                System.out.print("Please enter a 1 or 2\n");
            } else {
                fuellevel = true;
                switch ( fuel ) {
                case 1: fuelcharge = 40.00;
                fuelcharge+=Fullcharge;
                break;
                case 2: fuelcharge = 0.00;
                break;
                }
            }} catch (InputMismatchException ex) {
                System.out.println("Answer must be a number");
                in.next();
            }
        }

    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);
                nonTaxTotal = (nonTaxTotal + fuelcharge);
                total = (nonTaxTotal * 1.06);
                taxes = (total - nonTaxTotal);
                avrg = ( total/count );
                fullTotal+=total;
                numberOfDAysChosen = true;
            }
        } catch(InputMismatchException ex) {
            System.out.println("Answer must be a number");
            in.next();
        }
    }
    System.out.printf("Rental rate: $%.2f \n", dailyFee);
    System.out.printf("Total fuel charge: $%.2f \n", fuelcharge);
    System.out.printf("Subtotal: $%.2f \n", nonTaxTotal);
    System.out.printf("Taxes: $%.2f \n", taxes);
    System.out.printf("Total: $%.2f \n", fullTotal);

    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.print("End of application. \n");
                    System.out.println("Count of customers: " + count);
                    System.out.printf("Total fuel charges $ %.2f \n", Fullcharge);
                    System.out.printf("Total taxes collected: $ %.2f \n", taxes);
                    System.out.printf("Total of the Day: $ %.2f \n", fullTotal);
                    System.out.printf("The average bill was: $ %.2f \n", avrg);
                    System.exit(0);
            break;
            case 1: checkRunOrQuit = true;
            break;
            default:
                    System.out.println("Number must be either 1 or 0");
                    break;
        }} catch (InputMismatchException ex) {
            System.out.println("Invalid entry: ");
            in.next();
    }


    in.close();
    System.out.printf("Rental rate: $%.2f \n", dailyFee);
    System.out.printf("Total fuel charge: $%.2f \n", fuelcharge);
    System.out.printf("Subtotal: $%.2f \n", nonTaxTotal);
    System.out.printf("Total: $%.2f \n", fullTotal);
    System.out.printf("Taxes: $%.2f ", taxes);


    }

}
  1. 在输入租用的天数后,我希望用户能够被提升,如果他们想计算另一辆车。当我尝试标记并破坏它时,我无法成功地让代码从一开始就恢复(询问Car Type)??

共有1个答案

拓拔辰钊
2023-03-14

在写这篇文章之前,你必须认真考虑如何解决这个问题。你写的代码非常程序化,而且写得很糟糕。您应该真正了解如何在java中使用控制语句。

试试这个-

import java.util.InputMismatchException;
import java.util.Scanner;

public class Tester {

    public static void main(String[] args) {
        int days, cus, carType, fuel, count = 0;
        double dailyFee = 0, nonTaxTotal = 0, total = 0, fullTotal = 0, fuelcharge = 0, taxes = 0, avrg = 0, Fullcharge = 0;
        boolean checkRunOrQuit = false, chooseTypeVehicle = false, numberOfDAysChosen = false, fuellevel = false;
        Scanner in = new Scanner(System.in);
        while (true) {

            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.print("End of application\n");
                            System.out.println("Count of customers: " + count);
                            System.out.printf("Total fuel charges $ %.2f \n", Fullcharge);
                            System.out.printf("Total taxes collected: $ %.2f \n", taxes);
                            System.out.printf("Total of the Day: $ %.2f \n", fullTotal);
                            System.out.printf("The average bill was: $ %.2f \n", avrg);
                            System.exit(0);
                            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();
                }
            }

            checkRunOrQuit = false;

            while (!chooseTypeVehicle) {

                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;
                            count++;
                            break;
                        case 2:
                            dailyFee = 40.32;
                            count++;
                            break;
                        case 3:
                            dailyFee = 47.56;
                            count++;
                            break;
                        default:
                            System.out.print("Number must be 1-3\n");
                            chooseTypeVehicle = false;
                            break;
                    }
                } catch (InputMismatchException ex) {
                    System.out.println("Answer must be a number");
                    in.next();
                }
            }

            chooseTypeVehicle = false;

            while (!fuellevel) {
                try {
                    System.out.print("Is the fuel empty?\n");
                    System.out.println("Please enter 1 for yes.");
                    System.out.println("Please enter 2 for no.");
                    fuel = in.nextInt();
                    if (fuel <= 0 | fuel > 2.1) {
                        System.out.print("Please enter a 1 or 2\n");
                    } else {
                        fuellevel = true;
                        switch (fuel) {
                            case 1:
                                fuelcharge = 40.00;
                                fuelcharge += Fullcharge;
                                break;
                            case 2:
                                fuelcharge = 0.00;
                                break;
                        }
                    }
                } catch (InputMismatchException ex) {
                    System.out.println("Answer must be a number");
                    in.next();
                }
            }

            fuellevel = false;

            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);
                        nonTaxTotal = (nonTaxTotal + fuelcharge);
                        total = (nonTaxTotal * 1.06);
                        taxes = (total - nonTaxTotal);
                        avrg = (total / count);
                        fullTotal += total;
                        numberOfDAysChosen = true;
                    }
                } catch (InputMismatchException ex) {
                    System.out.println("Answer must be a number");
                    in.next();
                }
            }

            numberOfDAysChosen = false;

            System.out.printf("Rental rate: $%.2f \n", dailyFee);
            System.out.printf("Total fuel charge: $%.2f \n", fuelcharge);
            System.out.printf("Subtotal: $%.2f \n", nonTaxTotal);
            System.out.printf("Taxes: $%.2f \n", taxes);
            System.out.printf("Total: $%.2f \n", fullTotal);

        }
    }

}
 类似资料:
  • 你好,有人能帮我在java中使用我的当循环代码吗?我似乎在这里找不到解决方案,我希望有人能推荐或找到一个解决方案,我如何才能创建像(1, -2, 3, -4, 5, -6)这样的正确输出。谢谢!

  • 这是一个开关。通过点击操作让开关在“开”与“关”状态之间来回切换。 用法 Your browser does not support the video tag. 案例:小台灯 功能:按下按钮灯常亮,松开灯关闭

  • 这是一个开关。通过点击操作让开关在“开”与“关”状态之间来回切换。 用法 Your browser does not support the video tag. 案例:小台灯 功能:按下按钮灯常亮,松开灯关闭

  • 这一节专门讲各种开关。 在hi-nginx中,一共有6个开关: 系统缓存开关 用户缓存开关 header开关 cookie开关 session开关 tokens开关 系统缓存开关 hi_need_cache on|off; hi_cache_method GET|POST|PUT|HEAD; hi_cache_size 50; hi_cache_expires 5

  • 日安。 我的想法是: > 我需要递归地索引被索引实体的'name'成员+实体父实体的'name'+forther等,这样它们就会进入同一个lucene文档的字段。 然后,我将能够在一个字段上创建一个简单的查询,以检索所有需要的实体。 事实上,我对第1点有意见。@indexeDembedded不适合这里,因为循环关系和空前缀是禁止的(我不希望实体的名称和它们的父母在不同的字段中)。 是否可以以自定义

  • 不管是while还是for,所发起的循环,在python编程中是经常被用到的。特别是for,一般认为,它要比while快,而且也容易写(是否容易,可能因人而异,但是,执行时间快,是的确的),因此在实践中,for用的比较多点,不是说while就不用,比如前面所列举而得那个猜数字游戏,在业务逻辑上,用while就更容易理解(当然是限于那个游戏的业务需要而言)。另外,在某些情况下,for也不是简单地把对