-2

我想创建一个小型控制台程序,它可以从摄氏 - 华氏转换,反之亦然,这是我尝试过的代码:

import java.util.Scanner;

public class CelsiusFahrenheit {

    private static Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in);
        char reponse = 'N', choixConvert;

        do
        {
            do
            {
                System.out.println("Choisie le mode de converstion : ");
                System.out.println("1 - Converstisseur Celesuis - Fahrenheit");
                System.out.println("2 - Converstisseur Fahrenheit - Celesuis");

                do
                {
                    System.out.print("Votre Choix: ");
                    choixConvert = s.next().charAt(0);
                    if(choixConvert != '1' && choixConvert != '2')
                          System.out.println("Mode inconnu, veuillez réitérer votre choix.");
                }
                while(choixConvert!='1' && choixConvert!='2');

                System.out.print("Température à convertir : ");
                double temp = s.nextDouble();
                System.out.print("Resultat est: " + ConvertCelesuisFahrenheit(choixConvert,temp));
                System.out.println("\nSouhaitez-vous convertir une autre température ? (O/N)");
                reponse = Character.toUpperCase(s.nextLine().charAt(0));

            }while(reponse != 'O' && reponse != 'N');
        }while(reponse == 'O');

        System.out.println("Au revoir !");
    }

    private static Double ConvertCelesuisFahrenheit(int choixConvert,double temp )
    {
        if(choixConvert == 1)
            return (9/5)*temp+32;
        else
            return (temp-32)*(5/9);
    }
}

但是我遇到了一个问题,即函数 ConvertCelesuisFahrenheit() 总是返回 0.0 之后它给了我这个错误:

线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:CelsiusFahrenheit.main(CelsiusFahrenheit.java:33) 的 java.lang.String.charAt(Unknown Source) 处的 0

这是输出:

Choisie le mode de converstion : 
1 - Converstisseur Celesuis - Fahrenheit
2 - Converstisseur Fahrenheit - Celesuis
Votre Choix: 2
Température à convertir : 33
Resultat est: 0.0
Souhaitez-vous convertir une autre température ? (O/N)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at CelsiusFahrenheit.main(CelsiusFahrenheit.java:33)
4

3 回答 3

6

nextDouble()在输入流中留下换行符,所以当你然后调用

nextLine().charAt(0)

的结果nextLine()是一个空字符串。像上面那样从流中删除换行符,或者使用next()

于 2012-10-22T23:18:23.910 回答
2

这里有几个问题..

nextLine().charAt(0)获取空字符串,因为 nextDouble() 不使用新行

您可以通过简单地添加一个

String continueString = s.next();
response = Character.toUpperCase(continueString.charAt(0));

此外,您的转换计算中的数学是不正确的。使用5.0/9.0而不是5/9.

最后,您将choixConvert字符读取为 a char,然后将其作为 a 传递int给您的方法,因此该else块将始终执行。

于 2012-10-22T23:28:13.273 回答
1

更新这个reponse = Character.toUpperCase(s.nextLine().charAt(0));

          String line = s.nextLine();
          while(line.length() <1){
             line = s.nextLine();
          }
          reponse = Character.toUpperCase(line.charAt(0));
于 2012-10-22T23:21:50.887 回答