我正在为学校作业,并且已经有一个基本程序,可以通过询问总里程,汽车的平均 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。油已被合并到最终方程式中,无论其价值如何。
我不是在寻找任何人为我做我的任务。我想我想看看这是什么样子,或者如果有人有任何输入,我应该如何解决这个问题。
首先,我需要包含某种菜单,可以选择计算旅行费用或退出程序。
可以使用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 中调用此方法。
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);
}
}
}
您可以实现这样的退出选项:
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 + "."));
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(54条)