if(1) java_应该是关于java的if语句的一个问题

展开全部

由于这个题的判断比较多,所以最好分开写几个方法然后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;

}

}

运行结果为:

4384b9cd9c3a97b63dcbb4064a16e65a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值