Java 解决仅出现在一个编译器中的不匹配异常

Java 解决仅出现在一个编译器中的不匹配异常,java,while-loop,java.util.scanner,mismatch,inputmismatchexception,Java,While Loop,Java.util.scanner,Mismatch,Inputmismatchexception,这是一个在线作业。目标是多次接受两个输入(在此代码中为x和y变量),然后打印每个变量的最大值和最小值。代码如下: import java.io.*; import static java.lang.System.*; import java.util.Scanner; import java.lang.Math; class Lesson_20_Activity_One{ public static void main (String str[]) throws IOExcep

这是一个在线作业。目标是多次接受两个输入(在此代码中为x和y变量),然后打印每个变量的最大值和最小值。代码如下:

import java.io.*;
import static java.lang.System.*;

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


class Lesson_20_Activity_One{

     public static void main (String str[]) throws IOException {
          Scanner scan = new Scanner(System.in);
          double x; //the latitude input
          double y; //the longitude input
          double n = -90; //the maximum north value
          double e = -180; //the maximum east value
          double s = 90; //the maximum south value
          double w = 180; //the maximum west value
          int escape = 1; //the value that dictates when it's time to exit the while loop
          while (escape == 1) {
          System.out.println("Please enter the latitude:");
          x = scan.nextDouble();
          System.out.println("Please enter the longitude:");
          y = scan.nextDouble();
          if ((x>n) && (x<=90))
              n = x;
          if ((x<s) && (x>=-90))
              s = x;
          if ((y>e) && (y<=180))
              e = y;
          if ((y<w) && (y>=-180))
              w = y;
          if ((x>90)||(x<-90)||(y>180)||(y<-180))
              System.out.println("Incorrect Latitude or Longitude");
          System.out.println("Would you like to enter another location?");
          escape = scan.nextInt();
          }
          System.out.println("Farthest North: " + n);
          System.out.println("Farthest South: " + s);
          System.out.println("Farthest East: " + e);
          System.out.println("Farthest West: " + w);
     }

}

如何解决这些错误?我的扫描仪似乎有问题,但我不知道如何解决。

这可能是因为在线扫描仪对nextInt()的实现不同,导致了问题

可能尝试将输入解析为字符串,将输入解析为double/int会起作用:

Scanner scan = new Scanner(System.in);
double x; // the latitude input
double y; // the longitude input
double n = -90; // the maximum north value
double e = -180; // the maximum east value
double s = 90; // the maximum south value
double w = 180; // the maximum west value
int escape = 1; // the value that dictates when it's time to exit the
// while loop
while (escape == 1) {
    System.out.println("Please enter the latitude:");
    x = Double.parseDouble(scan.nextLine());
    System.out.println("Please enter the longitude:");
    y = Double.parseDouble(scan.nextLine());
    if ((x > n) && (x <= 90))
        n = x;
    if ((x < s) && (x >= -90))
        s = x;
    if ((y > e) && (y <= 180))
        e = y;
    if ((y < w) && (y >= -180))
        w = y;
    if ((x > 90) || (x < -90) || (y > 180) || (y < -180))
        System.out.println("Incorrect Latitude or Longitude");
    System.out.println("Would you like to enter another location?");
    escape = Integer.parseInt(scan.nextLine());
        }
System.out.println("Farthest North: " + n);
System.out.println("Farthest South: " + s);
System.out.println("Farthest East: " + e);
System.out.println("Farthest West: " + w);
Scanner scan=新的扫描仪(System.in);
双x;//纬度输入
双y;//经度输入
双n=-90;//最大北方值
双e=-180;//最大east值
双s=90;//最大南方值
双w=180;//最大西向值
int escape=1;//指示何时退出的值
//while循环
while(escape==1){
System.out.println(“请输入纬度:”);
x=Double.parseDouble(scan.nextLine());
System.out.println(“请输入经度:”);
y=Double.parseDouble(scan.nextLine());
如果((x>n)和&(x=-90))
s=x;
如果((y>e)和&(y=-180))
w=y;
如果((x>90)| |(x<-90)| |(y>180)| |(y<-180))
System.out.println(“不正确的纬度或经度”);
System.out.println(“您想输入其他位置吗?”);
escape=Integer.parseInt(scan.nextLine());
}
System.out.println(“最北端:+n”);
System.out.println(“最南端:+s”);
System.out.println(“最东边:+e”);
System.out.println(“最西边:+w”);

同样,这取决于在线扫描仪的工作方式。

这可能是因为在线扫描仪对nextInt()的实现不同,导致了问题

可能尝试将输入解析为字符串,将输入解析为double/int会起作用:

Scanner scan = new Scanner(System.in);
double x; // the latitude input
double y; // the longitude input
double n = -90; // the maximum north value
double e = -180; // the maximum east value
double s = 90; // the maximum south value
double w = 180; // the maximum west value
int escape = 1; // the value that dictates when it's time to exit the
// while loop
while (escape == 1) {
    System.out.println("Please enter the latitude:");
    x = Double.parseDouble(scan.nextLine());
    System.out.println("Please enter the longitude:");
    y = Double.parseDouble(scan.nextLine());
    if ((x > n) && (x <= 90))
        n = x;
    if ((x < s) && (x >= -90))
        s = x;
    if ((y > e) && (y <= 180))
        e = y;
    if ((y < w) && (y >= -180))
        w = y;
    if ((x > 90) || (x < -90) || (y > 180) || (y < -180))
        System.out.println("Incorrect Latitude or Longitude");
    System.out.println("Would you like to enter another location?");
    escape = Integer.parseInt(scan.nextLine());
        }
System.out.println("Farthest North: " + n);
System.out.println("Farthest South: " + s);
System.out.println("Farthest East: " + e);
System.out.println("Farthest West: " + w);
Scanner scan=新的扫描仪(System.in);
双x;//纬度输入
双y;//经度输入
双n=-90;//最大北方值
双e=-180;//最大east值
双s=90;//最大南方值
双w=180;//最大西向值
int escape=1;//指示何时退出的值
//while循环
while(escape==1){
System.out.println(“请输入纬度:”);
x=Double.parseDouble(scan.nextLine());
System.out.println(“请输入经度:”);
y=Double.parseDouble(scan.nextLine());
如果((x>n)和&(x=-90))
s=x;
如果((y>e)和&(y=-180))
w=y;
如果((x>90)| |(x<-90)| |(y>180)| |(y<-180))
System.out.println(“不正确的纬度或经度”);
System.out.println(“您想输入其他位置吗?”);
escape=Integer.parseInt(scan.nextLine());
}
System.out.println(“最北端:+n”);
System.out.println(“最南端:+s”);
System.out.println(“最东边:+e”);
System.out.println(“最西边:+w”);

同样,这取决于在线扫描仪的操作方式。

错误跟踪似乎表明调用
scan.nextInt()
时引发了
inputmaschException

打印后
System.out.println(“是否要输入其他位置?”)
,如果用户输入的不是整数,
scan.nextInt()
将引发异常。即使您按照其他人的建议将用户输入读取为字符串,
escape=Integer.parseInt(scan.nextLine())
中的parseInt方法也会抛出错误,因为该字符串可能不是有效的整数

我建议在
scan.nextInt()
调用周围添加如下try-catch块,这样当用户输入的不是有效整数(如数字8.238)时,程序不会崩溃


此代码将继续询问用户,直到用户输入有效的整数。

错误跟踪似乎表明调用
scan.nextInt()
时引发了
InputMismatchException

打印后
System.out.println(“是否要输入其他位置?”)
,如果用户输入的不是整数,
scan.nextInt()
将引发异常。即使您按照其他人的建议将用户输入读取为字符串,
escape=Integer.parseInt(scan.nextLine())
中的parseInt方法也会抛出错误,因为该字符串可能不是有效的整数

我建议在
scan.nextInt()
调用周围添加如下try-catch块,这样当用户输入的不是有效整数(如数字8.238)时,程序不会崩溃


此代码将继续询问用户,直到用户输入一个有效的整数。

您发送的是什么输入?此在线作业是否使用了一定数量的测试用例?例如,它可能会给你一个不是双精度的输入,也不是整数?你有没有试过向在线课程的技术支持人员询问(如果有)@FMC发送的输入是两个双精度值,一个x和y坐标。@RAZ_Muh_Taz测试用例只涉及双精度,据我所知。你在发送什么输入?这个在线作业有一定数量的测试用例吗?例如,它可能会给你一个不是双精度的输入,也不是整型的输入?你有没有试过向在线课程的技术支持人员询问(如果有)@FMC发送的输入是两个双精度值,一个x和y坐标。@RAZ_Muh_Taz据我所知,测试用例只涉及双精度。添加该代码会返回这个:“线程中的异常”“main”java.lang.NumberFormatException:对于输入字符串,“41.827”在java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)在java.lang.Integer.parseInt(Integer.java:580)在java.lang.Integer.parseInt(Integer.java:615)在第20课\u Activity.mai
boolean inputValid = false;
System.out.println("Would you like to enter another location?");
while (!inputValid) {
    try {
        escape = scan.nextInt();
        inputValid = true;
    } catch (InputMismatchException e) {
        inputValid = false;
        System.out.println("Please enter a valid Integer");
    }
}