簡體   English   中英

整數在比較中不能正常工作

[英]integers aren't working properly in a comparison

我目前在 12 年級計算機科學專業,如果解決方案非常明顯,我很抱歉,但我的老師也想不通。 所以基本上程序應該接受四個用戶輸入的輪胎壓力,然后如果前 2 不相等,或者后 2 不相等,或者任何輪胎不在一定范圍內,它將 output 文本告訴你出了什么問題。 它確實會告訴您正面或背面是否不相等,但不會告訴您它們中的任何一個是否超出范圍。

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");
        }
        
    }
}

編輯:你如何改變所有

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

if(VALUE < 32 || VALUE > 38)

讓我們為您的邏輯運行一些測試。

用戶輸入:

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

沒有范圍 output 因為這些數字在范圍內。

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

沒有inflation is not equal output 因為顯然它們都是平等的

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

后輪胎壓力相等,因此沒有inflation is not equal output。 盡管后輪胎高於 38,但它們被認為在此邏輯范圍內。

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

無 output

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM