水的状态 - 摄氏和华氏爪哇

时间:2016-01-18 15:40:12

标签: java temperature

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

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();

    }

谢谢你:)。

4 个答案:

答案 0 :(得分:1)

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

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

如果没有检测到任何内容,此代码将回退到Celsius。

答案 1 :(得分:1)

你总是得到最后一个字符并检查是'C'还是'F'

string.substring(string.length() - 1)

然后将其余的字符串转换为int变量

希望有所帮助。

答案 2 :(得分:1)

这是一个解决方案

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

输入有一种特定的格式,但您可以根据自己的喜好进行更改。

<强>输入55 c输出CELCIUS: Water is liquid at 55

在这里,您可以看到我们使用分隔符分割字符串的位置,并指定温度类型和温度本身。

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

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

答案 3 :(得分:0)

试试这个:

    //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();
    }