-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);
}
}
}
정확히 무엇을 입력하고 있습니까? – nhouser9
코드가 잘 작동합니다. 아마도 입력 내용이 문제일까요? 편집 : 어쩌면 "3.7"대신 "3,7"입력? –
내가 인텔리에서 뛰고있는 마일을 100.23으로 썼다. –