错误:线程“main”java.util.InputMismatchException中的异常

时间:2015-10-14 00:55:48

标签: java class math

所以我想弄清楚我的错误是什么,每当我运行这个程序时,它都会一直给我这个错误:

  

线程“main”java.util.InputMismatchException中的异常       at java.util.Scanner.throwFor(Scanner.java:864)       在java.util.Scanner.next(Scanner.java:1485)       在java.util.Scanner.nextInt(Scanner.java:2117)       在java.util.Scanner.nextInt(Scanner.java:2076)       在Lesson_20_Activity.main(Main.java:452)       在Ideone.assertRegex(Main.java:85)       在Ideone.assertRegex(Main.java:76)       在Ideone.test(Main.java:40)       在Ideone.main(Main.java:29)

这是我的代码。

import java.util.Scanner;
import java.lang.Math; 

class Lesson_20_Activity{
    public static void main(String[] args)
    {
      Scanner scan = new Scanner(System.in);
      double lat1 = 0;
      double long1 = 0;
      double lat2 = 0;
      double long2 = 0;
      double lat3 = 0;
      double long3 = 0;
      double lat4 = 0;
      double long4 = 0;
      double lat5 = 0;
      double long5 = 0;
      double lat6 = 0;
      double long6 = 0;
      double lat7 = 0;
      double long7 = 0;
      double lat8 = 0;
      double long8 = 0;
      double lat9 = 0;
      double long9 = 0;
      double lat10 = 0;
      double long10 = 0;
      int yesorno = 1;
      double north = 0;
      double south = 0;
      double east = 0;
      double west = 0;
      int correct1 = 1;
      int correct2 = 1;
      int correct3 = 1;
      int correct4 = 1;
      int correct5 = 1;
      int correct6 = 1;
      int correct7 = 1;
      int correct8 = 1;
      int correct9 = 1;
      int correct10 = 1;

    //Run 1
    if (yesorno == 1) {
    System.out.println("Please enter the latitide:");
    lat1 = scan.nextDouble();
    System.out.println("Please enter the longitude");
    long1 = scan.nextDouble();
    if (long1 >= 180 || long1 <= -180)
        System.out.println("Incorrect Latitude or Longitude");
        correct1 = 0;
    if (lat1 >= 90 || lat1 <= -90)
        System.out.println("Incorrect Latitude or Longitude");
        correct1 = 0;
    //Testing for the biggest north south east and west
    if (long1 > north)
        north = long1;
    if (long1 < south)
        south = long1;
    if (lat1 > east)
        east = lat1;
    if (lat1 < west)
        west = lat1;

    System.out.println("Would you like to enter another location?");
    yesorno = scan.nextInt();
    }
    //Printing the farthest distances
    if (yesorno != 1) {
        if (correct1 == 1)
            System.out.println("Farthest North: " + north);
            System.out.println("Farthest South: " + south);
            System.out.println("Farthest East: " + east);
            System.out.println("Farthest West: " + west);
    }

    //Run 2
    if (yesorno == 1) {
        System.out.println("Please enter the latitide:");
     lat2 = scan.nextDouble();
    System.out.println("Please enter the longitude");
    long2 = scan.nextDouble();
    if (long2 >= 180 || long2 <= -180)
        System.out.println("Incorrect Latitude or Longitude");
        correct2 = 0;
    if (lat2 >= 90 || lat2 <= -90)
        System.out.println("Incorrect Latitude or Longitude");
        correct2 = 0;
    //Testing for the biggest north south east and west
    if (long2 > north)
        north = long2;
    if (long2 < south)
        south = long2;
    if (lat2 > east)
        east = lat2;
    if (lat2 < west)
        west = lat2;

      System.out.println("Would you like to enter another location?");
      yesorno = scan.nextInt();
    }
   //Printing the farthest distances
    if (yesorno != 1) {
        if (correct2 == 1)
            System.out.println("Farthest North: " + north);
            System.out.println("Farthest South: " + south);
            System.out.println("Farthest East: " + east);
            System.out.println("Farthest West: " + west);
    }

    //Run 3
    if (yesorno == 1) {
        System.out.println("Please enter the latitide:");
    lat3 = scan.nextDouble();
    System.out.println("Please enter the longitude");
    long3 = scan.nextDouble();
    if (long3 >= 180 || long3 <= -180)
        System.out.println("Incorrect Latitude or Longitude");
        correct3 = 0;
    if (lat3 >= 90 || lat3 <= -90)
        System.out.println("Incorrect Latitude or Longitude");
        correct3 = 0;
    //Testing for the biggest north south east and west
    if (long3 > north)
        north = long3;
    if (long3 < south)
        south = long3;
    if (lat1 > east)
        east = lat3;
    if (lat1 < west)
        west = lat3;

    System.out.println("Would you like to enter another location?");
    yesorno = scan.nextInt();
    }   
    //Printing the farthest distances
    if (yesorno != 1) {
        if (correct3 == 1)
            System.out.println("Farthest North: " + north);
            System.out.println("Farthest South: " + south);
            System.out.println("Farthest East: " + east);
            System.out.println("Farthest West: " + west);
    }

    //Run 4
    if (yesorno == 1) {
        System.out.println("Please enter the latitide:");
    lat4 = scan.nextDouble();
    System.out.println("Please enter the longitude");
    long4 = scan.nextDouble();
    if (long4 >= 180 || long4 <= -180)
        System.out.println("Incorrect Latitude or Longitude");
        correct4 = 0;
    if (lat4 >= 90 || lat4 <= -90)
        System.out.println("Incorrect Latitude or Longitude");
        correct4 = 0;
    //Testing for the biggest north south east and west
    if (long4 > north)
        north = long4;
    if (long4 < south)
        south = long4;
    if (lat4 > east)
        east = lat4;
    if (lat4 < west)
        west = lat4;

    System.out.println("Would you like to enter another location?");
    yesorno = scan.nextInt();
    }
    //Printing the farthest distances
    if (yesorno != 1) {
        if (correct4 == 1)
        System.out.println("Farthest North: " + north);
        System.out.println("Farthest South: " + south);
        System.out.println("Farthest East: " + east);
        System.out.println("Farthest West: " + west);
    }

    //Run 5
    if (yesorno == 1) {
        System.out.println("Please enter the latitide:");
    lat5 = scan.nextDouble();
    System.out.println("Please enter the longitude");
    long5 = scan.nextDouble();
    if (long5 >= 180 || long5 >= -180)
        System.out.println("Incorrect Latitude or Longitude");
        correct5 = 0;
    if (lat5 >= 90 || lat5 <= -90)
        System.out.println("Incorrect Latitude or Longitude");
        correct5 = 0;
    //Testing for the biggest north south east and west
    if (long5 > north)
        north = long5;
    if (long5 < south)
        south = long5;
    if (lat5 > east)
        east = lat5;
    if (lat5 < west)
        west = lat5;

    System.out.println("Would you like to enter another location?");
    yesorno = scan.nextInt();
    }
    //Printing the farthest distances
    //Printing the farthest distances
    if (yesorno != 1) {
        if (correct5 == 1)
            System.out.println("Farthest North: " + north);
            System.out.println("Farthest South: " + south);
            System.out.println("Farthest East: " + east);
            System.out.println("Farthest West: " + west);
    }

    //Run 6
    if (yesorno == 1) {
        System.out.println("Please enter the latitide:");
    lat6 = scan.nextDouble();
    System.out.println("Please enter the longitude");
    long6 = scan.nextDouble();
    if (long6 >= 180 || long6 <= -180)
        System.out.println("Incorrect Latitude or Longitude");
        correct6 = 0;
    if (lat6 >= 90 || lat6 <= -90)
        System.out.println("Incorrect Latitude or Longitude");
        correct6 = 0;
    //Testing for the biggest north south east and west
    if (long6 > north)
        north = long6;
    if (long6 < south)
        south = long6;
    if (lat6 > east)
        east = lat6;
    if (lat6 < west)
        west = lat6;

    System.out.println("Would you like to enter another location?");
    yesorno = scan.nextInt();
    }
    //Printing the farthest distances
    if (yesorno != 1) {
        if (correct6 == 1)
            System.out.println("Farthest North: " + north);
            System.out.println("Farthest South: " + south);
            System.out.println("Farthest East: " + east);
            System.out.println("Farthest West: " + west);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你对#34的回答是什么?你想进入另一个地方吗? ? 扫描仪正在等待int&#34; nexInt()&#34;,你可能输入了一个角色?

答案 1 :(得分:0)

两种可能性:

  1. 您需要很长时间才能使用scan.nextDouble()。尝试:

    long1 = scan.nextLong();
    
    scan.nextLine();
    
  2. 您输入

    的整数
    scan.nextDouble();