我需要帮助解决我的布尔,我对编程非常陌生,我的教授的英文很差。简易Java布尔
我需要5添加到mpg
如果highway
是true
和减去2时city
是false
。
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,如果假 – Pexing
请至少格式正确启动码。缩进至关重要;这东西真的很难读**。你想要别人帮忙;所以至少可以让他们的工作尽可能简单。 – GhostCat
不,它增加5,然后减2。就像你指定的。条件不是相互排斥的。结果是+3的变化。 – zapl