创建旅行费用计算器

时间:2013-11-17 09:55:47

标签: java if-statement menu

我正在为学校工作,我已经有一个基本程序,通过询问总里程数,汽车平均每加仑汽油数和每加仑汽油的成本来计算旅行的总成本。这很好但我还需要添加一些项目,我不确定如何这样做。首先,我需要包含某种菜单,可以选择计算行程成本或退出程序。接下来我需要在程序中的某个地方加入一点询问用户是否想换油,然后根据答案将其添加到总计中。

import java.util.Scanner;
public class GasCalculator {

    public static void main (String args[])
    {
        Scanner scan = new Scanner(System.in);

        System.out.println("How many miles do you plan to travel?");
        int miles = scan.nextInt();
        System.out.println("So you will drive " + miles +" miles.");

        System.out.println("What is the price of gas?");
        double gas = scan.nextDouble();
        System.out.println("Ok, so gas costs $" + gas +" per gallon.");

        System.out.println("What is the average MPG of your car? Use the nearest whole number.");
        int mpg = scan.nextInt();
        System.out.println("So, you get " + mpg +" miles per gallon.");

        System.out.println("Would you like an oil change? Enter Y or N");


        double cost = (miles / mpg * gas + oil);

        System.out.println("Below is your total cost");
        System.out.println(String.format("Your trip will cost $" + cost + "."));

    }
}

正如你所看到的,我加了一点询问他们是否想换油。我这样做的愿景是为y或n答案创建变量,然后根据y或n创建if else语句。如果是,它将为新变量“oil”添加39.99。如果n,它将使变量为0.油已被纳入最终的等式,无论它的价值是否容易。

我不是在寻找任何人为我做任务。我想我希望看到这会是什么样的,或者如果有人有任何意见,我应该如何解决这个问题。感谢您提供任何帮助!

3 个答案:

答案 0 :(得分:2)

  

首先,我需要提供某种类型的菜单,提供选项   要么计算旅行费用,要么退出计划。

您可以使用switch声明。

//ask for user to enter 0 to exit, 1 to calculate the trip

switch(answer) {
     case 0 : System.exit(0);
              break;
     case 1 : //calculate cost trip here
              break;
     default : System.exit(0);
}
  

接下来我需要在程序中的某处加入一点询问是否   用户是否想换油,然后根据答案   将其添加到总数

您可以使用Scanner对象获取用户的值,并编写if语句来检查此值。

System.out.println("Would you like an oil change? Enter Y or N");
//here get the value of the user using your scanner object
double oil = 0;
if(/*test if value is equals to y*/)
    oil += 39.99;

<小时/> 提示:

  1. 要避免测试值是"y"还是"Y",请使用String类的equalsIgnoreCase方法。
  2. 当这将起作用时,您可以包含在方法中计算行程成本的函数,并在switch语句的情况1中调用此方法。

答案 1 :(得分:0)

您可以像这样实现退出选项:

System.out.println("Choose your desired option:\n1)Calculate Trip Cost\n2)Exit");
int answer = scan.nextInt();

if (answer==1){
    // the rest of your program
    System.out.println("How many miles do you plan to travel?");
    int miles = scan.nextInt();
    System.out.println("So you will drive " + miles +" miles.");

    System.out.println("What is the price of gas?");
    double gas = scan.nextDouble();
    System.out.println("Ok, so gas costs $" + gas +" per gallon.");

    System.out.println("What is the average MPG of your car? Use the nearest whole number.");
    int mpg = scan.nextInt();
    System.out.println("So, you get " + mpg +" miles per gallon.");

    System.out.println("Would you like an oil change? Enter Y or N");


    double cost = (miles / mpg * gas + oil);

    System.out.println("Below is your total cost");
    System.out.println(String.format("Your trip will cost $" + cost + "."));
}

答案 2 :(得分:0)

修改

import java.util.Scanner;

public class GasCalculator {

    public static void main (String args[])
    {

        double total = 0;       

        Scanner scan = new Scanner(System.in);

        System.out.println("How many miles do you plan to travel?");
        int miles = scan.nextInt();
        System.out.println("So you will drive " + miles +" miles.");

        System.out.println("What is the price of gas?");
        double gas = scan.nextDouble();
        System.out.println("Ok, so gas costs $" + gas +" per gallon.");

        System.out.println("What is the average MPG of your car? Use the nearest whole number.");
        int mpg = scan.nextInt();
        System.out.println("So, you get " + mpg +" miles per gallon.");

        System.out.println("Would you like an oil change? Enter Y or N");
        char oil = scan.next().charAt(0);
        if (Character.toUpperCase(oil) == 'Y'){
            System.out.println("Cost of oil change id 39.99);
            System.out.println("39.99 will be added to your total cost");
            total += 39.99;
        }

        double cost = (miles / mpg * gas);
        total += cost;

        String menu = "Pick a menu option: \n"
                      + "1. Calculate total \n"
                      + "2. exit";

        System.out.println(menu);
        int choice = scan.nextInt();
        if (choice == 1){
            System.out.println("Below is your total cost");
            System.out.println(String.format("Your trip will cost $" + total + "."));
        } else {
            System.exit(0);
        }
    }
}