Java – 当应该为假时,!String.equals(“abc”) 返回为真

huangapple go评论76阅读模式
英文:

Java - !String.equals("abc") returning TRUE when it should be FALSE

问题

我正在为学校解决这个问题(请忽略您可能看到的任何其他错误,我仍在努力解决中)。我在我的 while 循环中遇到了问题。

我正在努力添加一个检查,以防用户没有输入 cf,要求他们重新输入,直到他们输入这两个字母之一。

然而,在调试时,我发现函数在我的语句中忽略了 !,并且当 units 的值为 c 时返回 TRUE

public class TempConvert {
    
    // 转换从华氏温度到摄氏温度
    static int convToCelsius(int tempF) {
        int tempC = (tempF - 32) * 5 / 9;
        return tempC;
    }

    // 转换从摄氏温度到华氏温度
    static int convToFahrenheit(int tempC){
        int tempF = (tempC * 9 / 5) + 32;
        return tempF;
    }

    public static void main(String[] args){

        int temp;       // 用户指定的温度
        int convTemp;   // 转换后的温度变量
        String units;   // 用户指定的单位

        System.out.println("This program converts Temperatures from Fahrenheit to Celsius and vice versa.");
        System.out.print("Please enter your temperature: ");
        temp = TextIO.getlnInt();
        System.out.print("Please enter the units (F/C):");
        units = TextIO.getlnWord().toLowerCase();

        while (!units.equals("c") || !units.equals("f")){
            System.out.print("Please enter F for Fahrenheit or C for Celsius: ");
            units = TextIO.getlnWord();
        }

        if (units.equals("c")){
            convTemp = convToFahrenheit(temp);
            System.out.printf("A temperature of %d degrees Fahrenheit is equivalent to %d degrees Celsius", temp, convTemp);
        }

        else if (units.equals("f")){
            convTemp = convToCelsius(temp);
            System.out.printf("A temperature of %d degrees Celsius is equivalent to %d degrees Fahrenheit", temp, convTemp);
        }

    } // end main

} // end class
英文:

I am working on this problem for school (please ignore any other errors you may see, I'm still working through it). I am getting stuck on my while loop.

I'm working to add in a check for if the user doesn't input a c or f to re-ask them until they input one of those two letters.

However, as I debug it I am finding the function is ignoring the ! in my statement and returning TRUE when the value for units is c.

public class TempConvert {
// converts from fahrenheit to celsius
static int convToCelsius(int tempF) {
int tempC = (tempF - 32) * 5 / 9;
return tempC;
}
// converts from celsius to fahrenheit
static int convToFahrenheit(int tempC){
int tempF = (tempC * 9 / 5) + 32;
return tempF;
}
public static void main(String[] args){
int temp;       // user specified temp
int convTemp;   // var for converted temp
String units;   // user specified unit
System.out.println("This program converts Temperatures from Fahrenheit to Celsius and vice versa.");
System.out.print("Please enter your temperature: ");
temp = TextIO.getlnInt();
System.out.print("Please enter the units (F/C):");
units = TextIO.getlnWord().toLowerCase();
while (!units.equals("c") || !units.equals("f")){
System.out.print("Please enter F for Fahrenheit or C for Celsius: ");
units = TextIO.getlnWord();
}
if (units.equals("c")){
convTemp = convToFahrenheit(temp);
System.out.printf("A temperature of %d degrees Fahrenheit is equivalent to %d degrees Celsius", temp, convTemp);
}
else if (units.equals("f")){
convTemp = convToCelsius(temp);
System.out.printf("A temperature of %d degrees Celsius is equivalent to %d degrees Fahrenheit", temp, convTemp);
}
} // end main
} // end class

答案1

得分: 3

!units.equals("c") || !units.equals("f")
仔细阅读并思考一下。上述内容在无论 `units` 保存了什么值时都 __始终__ 为真。
假设它保存的是 `"c"`。
我们来代入计算:

!units.equals("c") || !units.equals("f")
!true || !false
false || true
true


我很确定你可能是想要的是 `!units.equals("c") && !units.equals("f")`,或者可能是 `!(units.equals("c") || units.equals("f"))`,它们实际上是等价的。
英文:

> !units.equals("c") || !units.equals("f")

Just go through it and think about it. The above is always true, regardless of what units holds.

Let's say it holds "c".

let's fill it in:

!units.equals("c") || !units.equals("f")
!true || !false
false || true
true

I'm pretty sure you meant !units.equals("c") && !units.equals("f") instead, or maybe !(units.equals("c") || units.equals("f")) which boils down to the same thing.

huangapple
  • 本文由 发表于 2020年9月16日 00:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63906043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定