Java-!String.equals“ abc”在应为FALSE时返回TRUE

问题描述

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

我正在努力检查用户是否没有输入cf来重新询问他们,直到他们输入这两个字母之一。

但是,在调试时,我发现函数忽略了我的语句中的!,并且当TRUE的值为units时返回了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",convTemp);
        }

    } // end main

} // end class

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)