问:做多个循环以及多个if语句和if-else语句|租赁汽车计算器项目

时间:2018-10-31 01:04:15

标签: java loops if-statement while-loop

我对该项目的说明如下:

说明:使用标记值循环。创建基本的租车计算器

向每个用户询问:

车辆类型(可以使用除字符串以外的其他方式,例如:1用于经济性,2用于轿车等。)租用天数计算(针对每个客户):

租金,税金,应付总额。共有三种不同的租金选项,分别收取不同的费用:经济型@ 31.76,轿车@ 40.32,SUV @ 47.56。 [注意:仅考虑全天单位(无小时费率)。

销售税为TOTAL的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;
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);
    }
}
  1. 我将如何使该程序循环回到提示用户:“按1进入“租金计算器”,否则按0退出\”。输入“租用天数”后?

[注意:输入了租用的天数后,我想要的是总金额而不是总金额。但是,我想要退出程序时的摘要信息。]

0 个答案:

没有答案