Java Celsius到Fahrenheit算法会引发StringIndexOutOfBoundsException

时间:2015-11-16 05:08:52

标签: java

您好我刚刚开始学习Java,我正在尝试从Celsius转换为Fahrenheit,反之亦然,但我的代码会抛出以下异常:

  var storeApp = angular.module('AngularStore', ['ngRoute']).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
    when('/store', { 
      templateUrl: 'partials/store.html',
      controller: 'storeController' }).
    when('/products/:productSku', {
      templateUrl: 'partials/product.html',
      controller: 'storeController' }).
    when('/cart', { 
      templateUrl: 'partials/shoppingCart.html',
      controller: 'storeController' }).
    otherwise({
      redirectTo: '/store' });
}]);

这是我的计划:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at hello.main(hello.java:33)

任何人都可以帮助我理解为什么我的代码会抛出public class hello { public static void main(String[] args) { System.out.println("CONVERTISSEUR DEGRES CELSIUS ET DEGRES FAHRENHEIT"); System.out.println("-------------------------------------------------"); Scanner sc = new Scanner(System.in); byte a = sc.nextByte(); char test = 'O'; while(test == 'O') { switch(a) { case 1: { System.out.println("Température a convertir :"); float temp=sc.nextFloat(); float nvtemp=temp * 9/5 + 32; System.out.println(temp + " °C correspond a : " + nvtemp + " °F"); }break; case 2: { System.out.println("Température a convertir :"); float temp = sc.nextFloat(); float nvtemp = (temp - 32) * 5/9; System.out.println(temp + " °F correspond a : " + nvtemp + " °C"); }break; default: System.out.println("Stp entrer 1 ou 2 !!"); } test = ' '; while(test != 'O' && test != 'N') { System.out.println("Souhaitez-vous convertir une autre température ?(O/N)"); test = sc.nextLine().charAt(0); } } System.out.println("Goodbye !!"); } } 吗?

1 个答案:

答案 0 :(得分:0)

在第二个while循环中,您正在调用test = sc.nextLine().charAt(0);sc.nextLine()用于读取之前nextXXX()方法调用留下的任何剩余字符。因此,不要在第二个while循环中使用test = sc.nextLine().charAt(0);使用test = sc.next().charAt(0);

即。以下代码可以使用

public class hello {

public static void main(String[] args) {
    System.out.println("CONVERTISSEUR DEGRES CELSIUS ET DEGRES FAHRENHEIT");
    System.out.println("-------------------------------------------------");
    System.out
            .println("Choisir le mode de conversion :\n1 - Convertisseur Celsius - Fahrenheit\n2 - Convertisseur Fahrenheit - Celsius");
    Scanner sc = new Scanner(System.in);
    byte a = sc.nextByte();
    char test = 'O';
    while (test == 'O') {
        switch (a) {
        case 1: {
            System.out.println("Température a convertir :");
            float temp = sc.nextFloat();
            float nvtemp = temp * 9 / 5 + 32;
            System.out.println(temp + " °C correspond a : " + nvtemp
                    + " °F");
        }
            break;
        case 2: {
            System.out.println("Température a convertir :");
            float temp = sc.nextFloat();
            float nvtemp = (temp - 32) * 5 / 9;
            System.out.println(temp + " °F correspond a : " + nvtemp
                    + " °C");
        }
            break;
        default:
            System.out.println("Stp entrer 1 ou 2  !!");
        }
        test = ' ';
        while (test != 'O' && test != 'N') {
            System.out
                    .println("Souhaitez-vous convertir une autre température ?(O/N)");
            test = sc.next().charAt(0);
        }
    }
    System.out.println("Goodbye !!");

}
}