Road trip:创建行程成本计算器(calculate gas cost road trip)

关于Road trip的问题,在calculate gas cost road trip中经常遇到, 我正在为学校作业,并且已经有一个基本程序,可以通过询问总里程,汽车的平均 mpg 和每加仑汽油的成本来计算旅行的总成本。这很有效,但是我还需要添加更多项目,并且我不确定如何执行。首先,我需要包含某种菜单,以提供答案来计算旅行总费用或退出程序。接下来,我需要在程序中的某个位置添加一个油

我正在为学校作业,并且已经有一个基本程序,可以通过询问总里程,汽车的平均 mpg 和每加仑汽油的成本来计算旅行的总成本。这很有效,但是我还需要添加更多项目,并且我不确定如何执行。首先,我需要包含某种菜单,以提供答案来计算旅行总费用或退出程序。接下来,我需要在程序中的某个位置添加一个油

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 语句。如果 y,它将为新变量“oil”添加 39.99。如果 n,它将使变量为 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;

为了避免测试值是否为"y""Y",请使用String类的方法equalsIgnoreCase

当这将工作时,您可以将计算行程成本的功能包装在一个方法中,并在 switch 语句的情况 1 中调用此方法。

1
Edit
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);
        }
    }
}
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 + "."));
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(242)
Python数组如何添加元素:通过添加元素减少数组(minimize array cost hackerrank)
上一篇
Ckd防爆电磁阀:Arduino控制 6种不同模式的电磁阀
下一篇

相关推荐

  • cvt和自动挡哪个好提升驾驶体验的最佳选择

    CVT(可变转换器)是一种由传动带及传动轮组成的无级变速器,它可以在没有传动档位的情况下,根据驾驶者的驾驶习惯,自动调整传动轮之间的转速比,从而实现无级变速。自动挡是指车辆自动变速箱,它使用液力传动来实现变速,可以自动适应车辆的行驶状态,减少司机的疲劳,提高行车的舒适性。…

    2023-06-08 07:42:19
    0 19 46
  • cv一叶扁舟和清影轩阳:漫游在一叶扁舟和清影轩阳之间

    cv一叶扁舟是一款开源的计算机视觉库,用于图像处理、计算机视觉等。它提供了丰富的API,可以帮助开发者快速实现各种图像处理任务,如图像分割、目标检测、图像识别等。清影轩阳是一款开源的计算机视觉框架,用于图像处理、目标检测、分类等。它提供了丰富的API,可以帮助开发者快速实现各种图像处理任务,如图像分割、目标检测、图像识别等。…

    2023-08-05 07:06:20
    0 55 40
  • css设置滚动条宽度设置:This is a title

    CSS设置滚动条宽度的方法:使用CSS3的。-webkit-属性:…

    2023-06-06 10:53:01
    0 58 54
  • xl和xe汽车cvt:探索XL和XE汽车的CVT技术优势

    XL和XE汽车CVT是一种变速器,它使用液力变矩器代替传统的机械变速器,以达到更高的效率。它的工作原理是,当发动机输出功率时,液力变矩器就会将这些功率转换为液压能量,然后将能量传递到变速器的输出轴上,从而实现变速。…

    2023-04-09 00:41:04
    0 98 58
  • cordon bleu是什么意思:法式炸鸡卷——Cordon Bleu的经典之作

    Cordon Bleu是一种烹饪技术,其中肉片被置于奶酪和火腿之间,然后用面包屑裹上,最后煎炸或烤熟。这种技术通常用于制作鸡肉,但也可以用于制作其他类型的肉类,如牛肉或猪肉。…

    2024-01-27 15:13:30
    0 95 78
  • countif 非空:非空单元格的计数

    Countif 非空是指计算某个单元格不为空的数量。代码如下:…

    2023-04-22 15:54:15
    0 85 67
  • java double保留一位小数:How to Round a Double to One Decimal Place in

    示例示例使用类可以轻松实现java double保留一位小数的功能,具体代码如下:// 创建对象…

    2023-05-14 07:40:03
    0 12 41
  • linux如何写c语言一个完整的指南

    示例示例在Linux系统中,可以使用C语言编写源代码文件,然后使用GCC编译器来编译源代码文件,生成可执行文件。下面是一个简单的C语言代码示例:…

    2023-05-16 02:02:45
    0 39 73

发表评论

登录 后才能评论

评论列表(54条)