【问题标题】:Easy Java Boolean简单的 Java 布尔值
【发布时间】:2016-05-23 14:22:23
【问题描述】:

我需要帮助来修复我的布尔值,我对编程很陌生,而且我教授的英语很差。

如果highwaytrue,我需要将5 加到mpg,并在cityfalse 时减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的变化。

标签: java boolean


【解决方案1】:

创建一个布尔变量,例如boolean inCity;

in.nextBoolean(); 更改为inCity = in.nextBoolean();

然后,在您获得输入值之后,您可以使用if 语句进行检查。在您的代码中,您在获得输入之前检查if 语句。

所以你会得到这个:

boolean inCity;
System.out.println("Are you in the city?(true/false): ");
inCity = in.nextBoolean(); //Store input in variable

if (inCity) { //If the condition is a boolean variable, you can type it like this(if (boolean) )
    mpg -= 2;//Same as mpg = mpg - 2;
} else { //if inCity is false:
    mpg += 5; //mpg = mpg + 5;
}

【讨论】:

  • 这是正确答案,我的不完整。 OP 的 if 语句的位置也不正确。
【解决方案2】:
if (highway) { 
    mpg = mpg + 5;
}

if (!city) {
    mpg = mpg - 2;
}

应该是

if (highway) { 
    mpg = mpg + 5;
} else { //not highway, so city 
    mpg = mpg - 2;
}

现在您检查它是否是高速公路。如果是true,代码加5。然后你检查它是否不是一个城市。如果是false,则代码减2。

另外,您只能在提交后检查布尔值:

System.out.println("Enter true if trip is on the highway false if the trip is in the city");
boolean highway = in.nextBoolean();
if (highway) { 
    mpg = mpg + 5;
} else { //not highway, so city 
    mpg = mpg - 2;
}

【讨论】:

  • 你说得对,我保留了他的格式,但最好也显示如何格式化:)
  • 最终解决方案只是在mpg上加5,即使他们输入false
  • 检查下面的...这不是您的代码中唯一的错误。我的观点是......如果您需要检查某人是否在城市或高速公路上,您可以使用一个布尔值来执行此操作,因为它有 2 个值。布尔高速公路=真;如果高速公路 == false 那么城市
  • @Turing,感谢额外的更正,但也许可以向 OP 解释发生了什么。 if (highway == true) 不是错误的陈述。
猜你喜欢
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 2010-10-06
  • 2010-10-11
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多