2016-09-30 4 views
-2

이 코드를 실행하고 첫 번째 변수에 double 값을 입력하면 (예 : 마일) 스레드 "main"java.util.InputMismatchException에서 예외로 read.nextDouble() 행에 오류가 표시됩니다. Javadoc에서두 변수를 입력하는 방법은 무엇입니까?

/** 
* Created by Ranjan Yadav on 1.10.2016. 
*/ 
public class GasMileage { 
    public static void main(String[] args){ 
     java.util.Scanner read = new java.util.Scanner(System.in); 
     int counter = 0; 
     System.out.println("Miles Driven(press 1 to quit): "); 
     double miles = read.nextDouble(); 
     double totalGalon = 0; 
     double totalMiles = 0; 
     double milesPerGalon = 0; 
     double totalMilesPerGalon = 0; 
     totalMiles += miles; 
     while(miles != 1){ 
      System.out.println("Gallon used: "); 
      double galon = read.nextDouble(); 
      counter++; 
      milesPerGalon = miles/galon; 
      totalMilesPerGalon += milesPerGalon; 
      System.out.println("Miles per gallon: " + milesPerGalon); 
      System.out.println("Miles Driven(press 1 to quit); "); 
      miles = read.nextDouble(); 
      totalGalon += galon; 
      totalMiles += miles; 
     } 
     if(counter == 0){ 
      System.out.println("No values were entered.\nThanks for Using!\n\n"); 
     }else{ 
      double avg = totalMilesPerGalon/counter; 
      System.out.printf("Total miles driven: %.2f" , totalMiles); 
      System.out.printf("Total gallons used: %.2f" , totalGalon); 
      System.out.printf("Miles per gallon for all trips: %.2f" , totalMilesPerGalon); 
     } 
    } 
} 
+0

정확히 무엇을 입력하고 있습니까? – nhouser9

+0

코드가 잘 작동합니다. 아마도 입력 내용이 문제일까요? 편집 : 어쩌면 "3.7"대신 "3,7"입력? –

+0

내가 인텔리에서 뛰고있는 마일을 100.23으로 썼다. –

답변

-1

:

예외 :

  • InputMismatchException - 다음의 토큰이 플로트 정규 표현식과 일치하지 않는 경우, 또는 범위를 벗어

기본적으로 you'r 숫자가 아닌 것을 입력하는 것.

관련 문제