【问题标题】:Looping with switchs?用开关循环?
【发布时间】:2019-04-04 05:49:56
【问题描述】:

我想做的是创建一个租车计算器。预装了三种不同汽车的每日费用和燃油费。我正试图将 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. 输入租用天数后,我希望提示用户是否要计算另一辆车。当我尝试标记和破坏它时,我无法成功让代码从头开始恢复(询问汽车类型)??

【问题讨论】:

  • while(!reset); - 不正确
  • @ScaryWombat 我不知道该怎么办我已经尝试解决这个问题好几个小时了。 :(
  • 是的,但是按照人们的建议尝试过吗?阅读我在这篇文章中的第一条评论 - 更正它 - 现在怎么办?
  • @ScaryWombat 是的,我已尽最大努力彻底阅读每条评论。删除while(!reset); 后,我仍然遇到同样的问题。我对重写整个程序而不产生更多错误的方法不够了解。我显然没有完全理解循环。如果不产生更多问题,我真的不知道该怎么做。我不理解休息。是的,我试过了。自从我第一次发帖以来,我没有离开我的电脑、java eclipse 或这个网站。我正在努力。

标签: java loops for-loop while-loop break


【解决方案1】:

在写它之前你必须真正考虑如何解决问题。您编写的代码非常程序化并且写得不好。您应该真正了解如何在 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);

        }
    }

}

【讨论】:

  • 是的,看看你的并将它与我的比较。我会说我的动手能力至少不符合这个项目的标准。对于未来的任何事情,我都会尝试更好地阅读这些章节(我的老师给我的),以及复习我们已经涵盖的一些章节。你对我应该关注什么有什么建议吗?无论如何,谢谢你的帮助。这确实解决了我的问题。请让我知道我是否能做的不仅仅是点赞和勾选。
猜你喜欢
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多