我在Java中更改该代码的过程时遇到了麻烦[已关闭]

ux6nzvsh  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(146)

6小时前关门了。
Improve this question
我在修改代码时遇到了麻烦。我想用布尔值来修改代码,但我不知道该怎么做。这让我很困惑。

    • 这是我的任务**

轮胎压力试验
检查所有轮胎的气压是否在35 - 45之间,如果有一个轮胎不在此范围内,则立即发出警告信息,之后程序继续读取和处理这些值。
如果出现警告消息,程序会在最后发出最终警告消息。为此,我们声明了一个布尔变量pressure OK。
布尔压力OK
我们将其初始化为true,如果轮胎超出有效范围,则将该值设置为false。
他们甚至帮助我在任务中布尔的东西,但我是如此的无能和困惑,我只是不知道如何做到这一点。老实说,我已经尝试了字面上的一切。在我问一个解决方案之前,我总是尝试一切可能的,因为如果我只是问一个解决方案而不尝试它,它将是无用的。

    • 我的密码:**
import java.util.Scanner;

public class Tyrepressure {

    public static void main(String[] args) {

        Scanner scanner1 = new Scanner(System.in);

        System.out.println("Enter your tyrepressure of your right-front-tyre in pounds per square inch: ");
        double rightFrontTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        System.out.println("Enter your tyrepressure of your left-front-tyre in pounds per square inch: ");
        double leftFrontTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        System.out.println("Enter your tyrepressure of your left-back-tyre in pounds per square inch: ");
        double rightBackTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        System.out.println("Enter your tyrepressure of your right-back-tyre in pounds per square inch: ");
        double leftBackTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        if (rightFrontTyre >= 35 && rightFrontTyre <= 45) {
            System.out.println("right-front-tyre = " + rightFrontTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("right-front-tyre = " + rightFrontTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (leftFrontTyre >= 35 && leftFrontTyre <= 45) {
            System.out.println("left-front-tyre = " + leftFrontTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("left-front-tyre = " + leftFrontTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (rightBackTyre >= 35 && rightBackTyre <= 45) {
            System.out.println("right-back-tyre = " + rightBackTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("right-back-tyre = " + rightBackTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (leftBackTyre >= 35 && leftBackTyre <= 45) {
            System.out.println("left-back-tyre = " + leftBackTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("left-back-tyre = " + leftBackTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (rightFrontTyre < 35 || rightFrontTyre > 45 || leftFrontTyre < 35 || leftFrontTyre > 45
                || rightBackTyre < 35 || rightBackTyre > 45 || leftBackTyre < 35 || leftBackTyre > 45) {
            System.out.println("You have to check your tyrepressure!");
        }
    }
}
    • 输出:**
Enter your tyrepressure of your right-front-tyre in pounds per square inch: 
40
 
Enter your tyrepressure of your left-front-tyre in pounds per square inch: 
32
 
Enter your tyrepressure of your left-back-tyre in pounds per square inch: 
11
 
Enter your tyrepressure of your right-back-tyre in pounds per square inch: 
40
 
right-front-tyre = 40.0 psi, your tyrepressure is okay!
 
left-front-tyre = 32.0 psi, your tyrepressure is critical, it is out of the allowed range!
 
right-back-tyre = 11.0 psi, your tyrepressure is critical, it is out of the allowed range!
 
left-back-tyre = 40.0 psi, your tyrepressure is okay!
 
You have to check your tyrepressure!

它工作正常,但我必须用布尔数据类型做最后一部分,正如我已经说过的,我只是不知道如何.....

1zmg4dgp

1zmg4dgp1#

您可以在if语句的条件中赋值表达式,然后在if语句中重用它,如下所示:

boolean pressureOkRightFront = (rightFrontTyre >= 35 && rightFrontTyre <= 45);

if (pressureOkRightFront) {
    System.out.println("right-front-tyre = " + rightFrontTyre + " psi, your tyrepressure is okay!");
} else {
    System.out.println("right-front-tyre = " + rightFrontTyre
            + " psi, your tyrepressure is critical, it is out of the allowed range!");
}

boolean pressureOkLeftFront = (leftFrontTyre >= 35 && leftFrontTyre <= 45);

// continue with the code for the other three tyres - remember to use a new boolean variable for each.

// then do this for the final check:

if (!(pressureOkRightFront && pressureOkLeftFront && 
        pressureOkRightBack && pressureOkLeftBack)) {
    System.out.println("You have to check your tyrepressure!");
}

请注意,在您的原始代码中,您检查了最后一次输出的相反条件-如果任何压力超出范围。我使用布尔逻辑从单个轮胎的单个预检查结果中获得相同的最终结果:
如果不是所有四个压力都正常,我打印警告。在这种情况下,NOT是感叹号!,括号很重要,因为否则!将仅针对pressureOkRightFront进行评估。

相关问题