展开全部
由于这个题的判断比较多,所以最好分开写几个方法然后636f70793231313335323631343130323136353331333337616466在 main 函数里调用,这样会使 main 函数里的代码比较简洁而且写起来也方便。以下方法可以参考:import java.util.Scanner;
public class TirePressureChecker {
private static int frontRight;
private static int frontLeft;
private static int rearRight;
private static int rearLeft;
private static boolean isBad;
public static void main(String[] args) {
String pressureString;
Scanner sn = new Scanner(System.in);
System.out.print("Front right tire pressure: ");
frontRight = sn.nextInt();
isBad = checkRange(frontRight);
System.out.print("Front left tire pressure: ");
frontLeft = sn.nextInt();
isBad = checkRange(frontLeft);
System.out.print("Rear right tire pressure: ");
rearRight = sn.nextInt();
isBad = checkRange(rearRight);
System.out.print("Rear left tire pressure: ");
rearLeft = sn.nextInt();
isBad = checkRange(rearLeft);
if (!isBad || checkEquality(frontRight, frontLeft,
rearRight, rearLeft) == 2) {
System.out.println("\nInflation is BAD");
} else if (checkEquality(frontRight, frontLeft,
rearRight, rearLeft) == 1) {
System.out.println("\nInflation is GOOD");
} else if (checkEquality(frontRight, frontLeft,
rearRight, rearLeft) == 3) {
System.out.println("\nInflation is OK");
}
sn.close();
}
public static boolean checkRange(int pressure) {
if (pressure <= 35 || pressure >= 45) {
System.out.println("WARNING: pressure is out of range");
return false;
}
return true;
}
public static int checkEquality(int fr, int fl,
int rr, int rl) {
if (fr == fl && fl == rr && rr == rl)
return 1;
else if (fr != fl || rr != rl)
return 2;
else if (fr == fl && rr == rl)
return 3;
return 0;
}
}
运行结果为: