我已经编写了代码(包括Haversine部分),如果用户仅提供整数作为输入,则可以正常工作。问题是,当用户在扫描仪中输入双精度字符(甚至声明了双精度变量的地方)时,出现此错误:
线程“主”中的异常java.util.InputMismatchException
我的代码如下:
import java.util.Scanner;
public class TripPlanner {
public static final double PI = 3.14159;
public static final double r = 6367.51;
public static void main(String[] args) {
greeting();
travelTimeAndBudget();
timeDifference();
countryArea();
howFar();
}
public static void greeting() {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Vacation Planner!");
System.out.print("What is your name? ");
String name = input.nextLine();
System.out.print("Nice to meet you " + name + ", where are you travelling to? ");
String place = input.nextLine();
System.out.println("Great! " + place + " sounds like a great trip!");
}
public static void travelTimeAndBudget() {
Scanner input = new Scanner(System.in);
int hours = 24; int minutes = hours*60;
System.out.print("How many days are you going to spend travelling? ");
int days = input.nextInt();
System.out.print("How much money, in USD, are you planning to spend on your trip? ");
double money = input.nextDouble();
System.out.print("What is the three letter currency symbol for your travel destination? ");
String currency = input.next();
System.out.print("How many " + currency + " are there in 1 USD? ");
double conversion = input.nextDouble();
System.out.println("If you are travelling for " + days + " days that is the same as " + (days*hours) + " hours or " + (days*minutes) + " minutes");
System.out.println("If you are going to spend $" + money + " USD that means per day you can spend up to $" + ((int)(100 * money / days) / 100.0) + "USD");
System.out.println("Your total budget in " + currency + " is " + (conversion*money) + " which per day is " + ((int)(100 * conversion * money / days) / 100.0));
}
public static void timeDifference() {
Scanner input = new Scanner(System.in);
System.out.print("What is the difference, in hours, between your home and your destination? ");
int hourDifference = input.nextInt();
System.out.println("That means that when it is midnight at home it will be " + hourDifference + ":00 in your travel destination and when it's noon at home it will be " + (hourDifference+12) + ":00");
}
public static void countryArea() {
Scanner input = new Scanner(System.in);
System.out.print("What is the square area of your destination country in km2? ");
double area = input.nextDouble();
System.out.println("In square miles that is " + ((int)(38.6102*area)/100.0));
}
public static void howFar() {
Scanner input = new Scanner(System.in);
System.out.print("What are the latitude coordinates in degrees of your destination city? (positive for north, negative for south) ");
double latDest = input.nextDouble();
System.out.print("What are the longitude coordinates in degrees of your destination city? (positive for east, negative for west) ");
double longDest = input.nextDouble();
System.out.print("What are the latitude coordinates in degrees of your home city? (positive for north, negative for south) ");
double latHome = input.nextDouble(); System.out.print("What are the longitude coordinates in degrees of your home city? (positive for east, negative for west) ");
double longHome = input.nextDouble();
latDest *= PI/180;
longDest *= PI/180;
latHome *= PI/180;
longHome *= PI/180;
double havLat = (1-Math.cos(latDest-latHome))/2;
double havLong = (1-Math.cos(longDest-longHome))/2;
double haversine = havLat + Math.cos(latHome) * Math.cos(latDest) * havLong;
double distance = 2 * r * Math.asin(Math.sqrt(haversine));
System.out.println("The distance between your home city and your destination is " + ((int)(distance*100)/100.0) + "km.");
}
这就是Scanner.nextInt()的工作方式。看看这个,例如:https://examples.javacodegeeks.com/java-basics/exceptions/java-util-inputmismatchexception-how-to-solve-inputmismatchexception/
要处理双精度和整数输入,您可以使用例如Scanner.nextDouble(),然后将其转换为整数。