简体   繁体   中英

integers aren't working properly in a comparison

I'm currently in grade 12 computer science so sorry if the solution is super obvious but my teacher couldn't figure it out either. so basically the program is supposed to take four user inputs for the tire pressure, then if the front 2 aren't equal, or the back 2 aren't equal, or any tire isn't within a certain range, it will output text telling you what's wrong. it does tell you if the front or back isn't equal, but it doesn't tell you if any of them are out of range.

class TireCheck{
    public static void main (String[] args) throws IOException{
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(inStream);
        String frontLeft, frontRight, backLeft, backRight;
        
        System.out.println("Enter front left tire pressure:"); //asks for user input
        frontLeft = stdin.readLine();
        
        System.out.println("Enter front right tire pressure:"); //asks for user input
        frontRight = stdin.readLine();
        
        System.out.println("Enter back left tire pressure:"); //asks for user input
        backLeft = stdin.readLine();
        
        System.out.println("Enter back right tire pressure:"); //asks for user input
        backRight = stdin.readLine();
        
        int FrontLeft = Integer.parseInt(frontLeft);
        int FrontRight = Integer.parseInt(frontRight);
        int BackLeft = Integer.parseInt(backLeft);
        int BackRight = Integer.parseInt(backRight);
        
        if( FrontLeft >= 32 && FrontLeft <= 38 ){
            System.out.println("Tire out of range: front left");
        }
        if(FrontRight >= 32 && FrontRight <= 38){
            System.out.println("Tire out of range: front right");
        }
        if(BackLeft >= 32 && BackLeft <= 38){
            System.out.println("Tire out of range: back left");
        }
        if(BackRight >= 32 && BackRight <= 38){
            System.out.println("Tire out of range: back right");
        }
        if(FrontLeft != FrontRight){
            System.out.println("Tire inflation is not equal: front");
        }
        if(BackLeft != BackRight){
            System.out.println("Tire inflation is not equal: back");
        }
        
    }
}

Edited: How about you change all

if(VALUE >= 32 && VALUE <= 38)

to

if(VALUE < 32 || VALUE > 38) ?

Let's run some tests for the logic you have.

User enters:

Front Left: 30
Front Right: 31
Back Left: 30
Back Right: 31

output: 
Tire inflation is not equal: front
Tire inflation is not equal: back

No range output because those numbers are in range.

Front Left: 32
Front Right: 32
Back Left: 32
Back Right: 32

output: 
Tire out of range: front left
Tire out of range: front right
Tire out of range: back left
Tire out of range: back right

No inflation is not equal output because obviously they are all equal

Front Left: 36
Front Right: 38
Back Left: 40
Back Right: 40

output: 
Tire out of range: front left
Tire out of range: front right
Tire inflation is not equal: front

Back tire pressures are equal so no inflation is not equal output. Although back tires are above 38, they are considered in range for this logic.

Front Left: 45
Front Right: 45
Back Left: 29
Back Right: 29

No output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM