Java 水的状态-摄氏度和华氏度

Java 水的状态-摄氏度和华氏度,java,temperature,Java,Temperature,基本上,任务是:我应该输入一个温度值,后跟一个空格,字母C表示摄氏度,字母F表示华氏度,例如:“-12摄氏度”或“165华氏度”,输出应该是水在输入温度下的状态。冰的熔点是在32华氏度,水的沸点是212华氏度。我一直在想如何获得Celcius或华氏度的输入,以及如何更新我下面的代码以适应华氏度的沸点。到目前为止,我的代码是: public static void stateOfWater(){ System.out.println("Please enter a temperature

基本上,任务是:我应该输入一个温度值,后跟一个空格,字母C表示摄氏度,字母F表示华氏度,例如:“-12摄氏度”或“165华氏度”,输出应该是水在输入温度下的状态。冰的熔点是在32华氏度,水的沸点是212华氏度。我一直在想如何获得Celcius或华氏度的输入,以及如何更新我下面的代码以适应华氏度的沸点。到目前为止,我的代码是:

public static void stateOfWater(){
     System.out.println("Please enter a temperature value in Celcius or Fahrenheit : ");
    Scanner input = new Scanner (System.in);
    int x = input.nextInt(); 

    if (x <= 0)
        System.out.println("Water is solid at " + x );

    else if (100 <= x )
        System.out.println("Water is gaseous at " + x );

    else 
        System.out.println("Water is liquid at " + x  );
    input.close();

    }
public static void stateOfWater(){
System.out.println(“请输入摄氏度或华氏度的温度值:”);
扫描仪输入=新扫描仪(System.in);
int x=input.nextInt();

如果(x您需要检测输入是摄氏还是华氏。一种方法是:

String str = input.nextString();
if (str.contains("F") || str.contains("f") {
    //It's fahrenheit. process accordingly.
} else {
    //default to celsius.  process accordingly.
}

如果没有检测到任何东西,此代码将返回摄氏度。

您需要检测输入是摄氏度还是华氏度。一种方法是:

String str = input.nextString();
if (str.contains("F") || str.contains("f") {
    //It's fahrenheit. process accordingly.
} else {
    //default to celsius.  process accordingly.
}

如果没有检测到任何东西,此代码将回落到摄氏度。

您将始终获取最后一个字符,并检查这是“C”还是“F”

string.substring(string.length() - 1)
然后将字符串的其余部分强制转换为int变量


希望这能有所帮助。

您可以始终获取最后一个字符,并检查它是“C”还是“F”

string.substring(string.length() - 1)
然后将字符串的其余部分强制转换为int变量

希望能有所帮助。

试试这个:

    //Get the input from user
    String inputStr = input.nextLine();

    //Find the space
    int indexOfSpace = inputStr.lastIndexOf(" ");

    //Read the temperature value
    int x = Integer.valueOf(inputStr.substring(0, indexOfSpace));

    //figure out the scale being used
    String scale = inputStr.substring(indexOfSpace+1);
    if(scale.equalsIgnoreCase("C")){
        //handle Celcius input
    } else if(scale.equalsIgnoreCase("F")){
        //handle Fahrenheit input
    } else{
        throw new IllegalArgumentException();
    }
试试这个:

    //Get the input from user
    String inputStr = input.nextLine();

    //Find the space
    int indexOfSpace = inputStr.lastIndexOf(" ");

    //Read the temperature value
    int x = Integer.valueOf(inputStr.substring(0, indexOfSpace));

    //figure out the scale being used
    String scale = inputStr.substring(indexOfSpace+1);
    if(scale.equalsIgnoreCase("C")){
        //handle Celcius input
    } else if(scale.equalsIgnoreCase("F")){
        //handle Fahrenheit input
    } else{
        throw new IllegalArgumentException();
    }
这里有一个解决方案

   public static void stateOfWater() {
        System.out.println("Please enter a temperature value in Celcius or Fahrenheit : ");
        Scanner s = new Scanner(System.in);

        String[] argv = s.nextLine().split(" ");

        int temp = Integer.parseInt(argv[0]);
        char tempType = argv[1].charAt(0);

        switch (tempType) {
            case 'c':
                if (temp <= 0) {
                    System.out.println("CELCIUS: Water is solid at " + temp);
                } else if (temp >= 100) {
                    System.out.println("CELCIUS: Water is gaseous at " + temp);
                } else {
                    System.out.println("CELCIUS: Water is liquid at " + temp);
                }
                break;

            case 'f':
                if (temp <= 32) {
                    System.out.println("FAHRENHEIT: Water is solid at " + temp);
                } else if (temp >= 212) {
                    System.out.println("FAHRENHEIT: Water is gaseous at " + temp);
                } else {
                    System.out.println("FAHRENHEIT: Water is liquid at " + temp);
                }
                break;
        }
    }
这里有一个解决方案

   public static void stateOfWater() {
        System.out.println("Please enter a temperature value in Celcius or Fahrenheit : ");
        Scanner s = new Scanner(System.in);

        String[] argv = s.nextLine().split(" ");

        int temp = Integer.parseInt(argv[0]);
        char tempType = argv[1].charAt(0);

        switch (tempType) {
            case 'c':
                if (temp <= 0) {
                    System.out.println("CELCIUS: Water is solid at " + temp);
                } else if (temp >= 100) {
                    System.out.println("CELCIUS: Water is gaseous at " + temp);
                } else {
                    System.out.println("CELCIUS: Water is liquid at " + temp);
                }
                break;

            case 'f':
                if (temp <= 32) {
                    System.out.println("FAHRENHEIT: Water is solid at " + temp);
                } else if (temp >= 212) {
                    System.out.println("FAHRENHEIT: Water is gaseous at " + temp);
                } else {
                    System.out.println("FAHRENHEIT: Water is liquid at " + temp);
                }
                break;
        }
    }

将输入处理为temp和scale,然后
if(((temp<0)和&(scale='c'))|&(temp<32)和&(scale='f')){…solid…}
非常感谢大家:)。它对我有效,我现在已经对它进行了排序谢谢:)。将输入处理为temp和scale,然后
if((temp<0)和(scale='c'))|(temp<32)&&(scale=='f'){…solid…}
非常感谢你们:)。它对我很有效,我现在已经整理好了。谢谢:)。