只有在为真时才询问变量

时间:2013-10-15 22:45:44

标签: java project computer-science

我正在为我的CSE 20课程做第一个项目,我必须编写一个简单的租车系统。我一开始就陷入困境。我需要问车,#天,然后你是否是俱乐部会员。如果你的代码不需要继续并输出总数,那就是它。但如果您是俱乐部会员,则必须提出另一个关于高管包的问题,​​然后这会增加另一条线来计算总产量。我不知道如何编写它来完全运行。任何帮助将不胜感激。

import java.util.Scanner;

public abstract class Bobcar1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner blah = new Scanner(System.in);
    System.out.println("Available cars: 1 for Econobox, 2 for Compact, 3 for Standard");
    System.out.print("Please choose the rental car: ");
    int car;
    car = blah.nextInt();
    System.out.print("Please enter the number of rental days: ");
    int day;
    day = blah.nextInt();
    System.out.print("Club member?: 1 for yes, 0 for no: ");
    int re;
    re = blah.nextInt();
    if (re == 1)
        System.out.print("Platinum Executive Package?: 1 for yes, 0 for no: ");
    int pack;
    pack = blah.nextInt();
    if (re == 0) then what do i code here to get the program to run through completely?

    if (car == 1)   {
            int car1;
            car1 = (25 * day);

                    System.out.println("Base: " + day + " day(s) for a Econobox @ $25 per day:      " + "+$" + car1);
                    if (pack == 0 && re == 0)       {
                    System.out.println();
                    System.out.println("Total Estimate for Rental:                  " + " $" + car1);
                    }
                    if (re == 1 && pack == 0)       {
                            int re1;
                            re1 = (day/5)*25;
                    System.out.println("Club Member Discount:                   " + "-$" + re1);
                            int total1r;
                            total1r = car1 - re1;
                    System.out.println();
                    System.out.println("Total Estimate for Rental:                  " + " $" + total1r);
                    }
                    if (pack == 1 && re == 1)       {
                            int pack1;
                            pack1 = (int) (car1*.20);
                    System.out.println("Platinum Executive Package:                 " + "+$" + pack1);     
                            int re1;
                            re1 = (day/5)*25;
                    System.out.println("Club Member Discount:                   " + "-$" + re1);
                            int total1;
                            total1 = car1 + pack1 - re1;
                            System.out.println("Total Estimate for Rental:                  " + " $" + total1);
                    }
    }
    if (car == 2)   {
            int car1;
            car1 = (55 * day);

                    System.out.println("Base: " + day + " day(s) for a Compact @ $55 per day:  " + "+$" + car1);
                    if (pack == 0 && re == 0)       {
                    System.out.println();
                    System.out.println("Total Estimate for Rental:                  " + " $" + car1);
                    }
                    if (re == 1 && pack == 0)       {
                            int re1;
                            re1 = (day/5)*55;
                    System.out.println("Club Member Discountt:                   " + "-$" + re1);
                            int total1r;
                            total1r = car1 - re1;
                    System.out.println();
                    System.out.println("Total Estimate for Rental:                  " + " $" + total1r);
                    }
                    if (pack == 1 && re == 1)       {
                            int pack1;
                            pack1 = (int) (car1* .20);
                    System.out.println("Platinum Executive Package:                 " + "+$" + pack1);     
                            int re1;
                            re1 = (day/5)*55;
                    System.out.println("Club Member Discount:                   " + "-$" + re1);
                            int total1;
                            total1 = car1 + pack1 - re1;
                            System.out.println("Total Estimate for Rental:                  " + " $" + total1);
                    }
    }
    if (car == 3)   {
            int car1;
            car1 = (100 * day);

                    System.out.println("Base: " + day + " day(s) for a Standard @ $100 per day:        " + "+$" + car1);
                    if (pack == 0 && re == 0)       {
                    System.out.println();
                    System.out.println("Total Estimate for Rental:                  " + " $" + car1);
                    }
                    if (re == 1 && pack == 0)       {
                            int re1;
                            re1 = (day/5)*100;
                    System.out.println("Return Customer Discount:                   " + "-$" + re1);
                            int total1r;
                            total1r = car1 - re1;
                    System.out.println();
                    System.out.println("Total Estimate for Rental:                  " + " $" + total1r);
                    }
                    if (pack == 1 && re == 1)       {
                            int pack1;
                            pack1 = (int) (car1*.20);
                    System.out.println("Platinum Executive Package:                 " + "+$" + pack1);     
                            int re1;
                            re1 = (day/5)*100;
                    System.out.println("Club Member Discount:                   " + "-$" + re1);
                            int total1;
                            total1 = car1 + pack1 - re1;
                            System.out.println("Total Estimate for Rental:                  " + " $" + total1);
    }

 }}}}

1 个答案:

答案 0 :(得分:0)

您应该修改您的OP(原始帖子)以包含您在评论中发布的问题。

如果您想要做的只是修改代码,只询问他们是否需要铂金包装,如果他们指定他们是俱乐部会员,那么您需要修改您的代码,以便您只读取扫描仪的输入如果你打印这个问题那个问题。

换句话说,在if语句块周围添加{}。这是最佳实践,即使对于1行语句块也是如此。

您目前有:

...
int re;
re = blah.nextInt();
if (re == 1)
    System.out.print("Platinum Executive Package?: 1 for yes, 0 for no: ");
int pack;
pack = blah.nextInt();
...

您可以像这样修改代码:

...
int pack;
int re = blah.nextInt();
if (re == 1) {
    System.out.print("Platinum Executive Package?: 1 for yes, 0 for no: ");
    pack = blah.nextInt();
}
...