应该是关于java的if语句的一个问题
发布网友
发布时间:2022-05-07 12:21
我来回答
共2个回答
热心网友
时间:2023-10-30 16:48
由于这个题的判断比较多,所以最好分开写几个方法然后在 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;
}
}
运行结果为:
热心网友
时间:2023-10-30 16:48
不知道说的啥意思=-=
难道是这个:int a=1==1?1:2;
???追问就是要求在输入 气压数值后,程序会判断这个数值 有没有超出范围 ,并在输入框下 列出,就像我给的照片一样
就是这里我不是非常懂该怎样写程序。
热心网友
时间:2023-10-30 16:48
由于这个题的判断比较多,所以最好分开写几个方法然后在 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;
}
}
运行结果为:
热心网友
时间:2023-10-30 16:48
由于这个题的判断比较多,所以最好分开写几个方法然后在 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;
}
}
运行结果为:
热心网友
时间:2023-10-30 16:48
不知道说的啥意思=-=
难道是这个:int a=1==1?1:2;
???追问就是要求在输入 气压数值后,程序会判断这个数值 有没有超出范围 ,并在输入框下 列出,就像我给的照片一样
就是这里我不是非常懂该怎样写程序。
热心网友
时间:2023-10-30 16:48
不知道说的啥意思=-=
难道是这个:int a=1==1?1:2;
???追问就是要求在输入 气压数值后,程序会判断这个数值 有没有超出范围 ,并在输入框下 列出,就像我给的照片一样
就是这里我不是非常懂该怎样写程序。
热心网友
时间:2023-10-30 16:48
由于这个题的判断比较多,所以最好分开写几个方法然后在 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;
}
}
运行结果为:
热心网友
时间:2023-10-30 16:48
不知道说的啥意思=-=
难道是这个:int a=1==1?1:2;
???追问就是要求在输入 气压数值后,程序会判断这个数值 有没有超出范围 ,并在输入框下 列出,就像我给的照片一样
就是这里我不是非常懂该怎样写程序。
热心网友
时间:2023-10-30 16:48
由于这个题的判断比较多,所以最好分开写几个方法然后在 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;
}
}
运行结果为:
热心网友
时间:2023-10-30 16:48
不知道说的啥意思=-=
难道是这个:int a=1==1?1:2;
???追问就是要求在输入 气压数值后,程序会判断这个数值 有没有超出范围 ,并在输入框下 列出,就像我给的照片一样
就是这里我不是非常懂该怎样写程序。