提问人:Tomas Bernatonis 提问时间:1/24/2023 最后编辑:shmoselTomas Bernatonis 更新时间:1/24/2023 访问量:66
我在 Java 中更改该代码的进程时遇到问题 [已关闭]
I have trouble with changing the process of that code in Java [closed]
问:
我在更改代码时遇到问题。我想用布尔值来做到这一点,但我不知道该怎么做。这让我感到困惑。
这是我的任务:
胎压试验
就是要检查所有轮胎的压力是否在35到45之间。如果轮胎超出此范围,则会立即发出警告消息。之后,程序继续读取和处理值。
如果出现警告消息,程序将在末尾发出最后一条警告消息。为此,我们声明了一个布尔变量 pressureOK。
布尔值压力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!
它工作正常,但我必须使用布尔数据类型完成最后一部分,正如我已经说过的,我只是不知道如何做.....
答:
0赞
cyberbrain
1/24/2023
#1
您可以在语句的条件中分配表达式 - 然后在 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
评论
0赞
Tomas Bernatonis
1/24/2023
谢谢!但我还有一个问题。在哪里可以看到布尔值是真还是假。还是布尔值自动将其设置为 true?难道我不必在 if 语句中做:if (pressureOkRightFront == true ) 吗?
0赞
cyberbrain
1/24/2023
不,实际上是一种糟糕的编码风格(但会 ocmpile)。 只接受计算结果为 的表达式,所以你永远无法编译 如果你使用调试器,你可以看到布尔值,或者打印它 - 或者你的意思是别的什么?您可以在此(旧但仍然有效)教程中找到原始数据类型的概述:docs.oracle.com/javase/tutorial/java/nutsandbolts/...if (pressureOkRightFront == true )
if
boolean
if (3) {...
boolean
评论