【发布时间】:2016-05-23 14:22:23
【问题描述】:
我需要帮助来修复我的布尔值,我对编程很陌生,而且我教授的英语很差。
如果highway 是true,我需要将5 加到mpg,并在city 是false 时减2。
import java.util.Scanner;
public class AdvancedTripCalc {
public static void main(String[] args) {
// New Trip Calc.
String firstname;
int mpg;
int miles;
double price;
double totalcost;
Scanner in = new Scanner(System.in);
System.out.println("Please enter your First Name");
firstname = in.nextLine();
System.out.println("Please enter the MPG of your car");
mpg = in.nextInt();
boolean highway = true;
boolean city = false;
if (highway == true) {
mpg = mpg + 5;
}
if (city == false) {
mpg = mpg - 2;
}
System.out.println("Enter true if trip is on the highway false if the trip is in the city");
in.nextBoolean();
System.out.println("Please enter the Miles to be traveled");
miles = in.nextInt();
System.out.println("Please enter the price per gallon of gas");
price = in.nextDouble();
totalcost = miles * price / mpg;
System.out.println("Your name is " + firstname);
System.out.println("Your MPG is " + mpg);
System.out.println("Your miles to be traveled is " + miles);
System.out.println("Your price per gallon of gas is " + price);
System.out.println("Your total cost is " + totalcost);
}
}
【问题讨论】:
-
布尔值现在将 3 添加到 mpg,但如果为真,我需要它加 5,如果为假,我需要减 2
-
请至少正确格式化您的代码。缩进很重要;这东西真的很难阅读。你希望别人帮忙;所以至少让他们的工作尽可能轻松。
-
不,它加 5,然后减 2。就像您指定的那样。这些条件不是相互排斥的。结果是+3的变化。