线程"main"中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

Ich*_*aki -2 java

我想创建一个小的控制台程序,从Celsius - Fahrenheit转换,反之亦然,这是我尝试的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我有一个问题,函数ConvertCelesuisFahrenheit()总是返回0.0,然后它给我这个错误:

线程"main"中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:在CelsiusFahrenheit.main的java.lang.String.charAt(未知来源)处为0(CelsiusFahrenheit.java:33)

这是输出:

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)
Run Code Online (Sandbox Code Playgroud)

Dan*_*her 6

nextDouble() 将换行留在输入流中,所以当你调用时

nextLine().charAt(0)
Run Code Online (Sandbox Code Playgroud)

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