简体   繁体   English

有没有人可以帮助我解决这个运行时错误?

[英]Is there anyone that can help me with this Runtime error?

I am completely new to programming.I created a program that uses two coordinates entered by the user and calculates the gradient and also comments when the function is increasing or decreasing just like in Mathematics Functions.I get a runtime error whenever i enter a fraction in this format 9/4.I我对编程完全陌生。我创建了一个程序,它使用用户输入的两个坐标并计算梯度,并在 function 像在数学函数中一样增加或减少时发表评论。每当我输入一个分数时,我都会遇到运行时错误这种格式 9/4.I

import java.util.*;
public class mathfunctions {
public static void main(String[] args) {
Scanner coordinates = new Scanner (System.in).useLocale(Locale.US);
System.out.println("Enter 2nd y-coordinate:");

Double y2 = coordinates.nextDouble();

System.out.println("Enter 1st y-coordinate:");
Double y1 = coordinates.nextDouble();
System.out.println("Enter 2nd x-coordinate:");
Double x2 = coordinates.nextDouble();
System.out.println("Enter 1st x-coordinate:");

Double x1 = coordinates.nextDouble();
double numerator= (y2-y1);
double denominator= (x2-x1);
double gradient= numerator/denominator;
if
(numerator==0){
 System.out.print("Math Error!!!"); 
}
else{
System.out.print("Gradient=");
System.out.println(gradient);
}
if 
(gradient<0){
  System.out.println("The function is decreasing");    
}
else{
  System.out.println("The function is increasing");
}
  }
/*****************************************
This simple code is used to calculate the slope/gradient also referred to rate of change
*****************************************/
}

The error错误

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:876)
    at java.util.Scanner.next(Scanner.java:1502)
    at java.util.Scanner.nextDouble(Scanner.java:2433)
    at com.mathfunctions.mathfunctions.main(mathfunctions.java:8)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.duy.android.compiler.java.Java.run(Java.java:115)
    at com.duy.ide.javaide.run.activities.ExecuteActivity.executeDex(ExecuteActivity.java:147)
    at com.duy.ide.javaide.run.activities.ExecuteActivity.exec(ExecuteActivity.java:124)
    at com.duy.ide.javaide.run.activities.ExecuteActivity.access$100(ExecuteActivity.java:45)
    at com.duy.ide.javaide.run.activities.ExecuteActivity$1.run(ExecuteActivity.java:88)
    at java.lang.Thread.run(Thread.java:764)

This exception is usually thrown when the user input does not match the expected input.当用户输入与预期输入不匹配时,通常会引发此异常。 I do not think you are supposed to give a fraction format input.我认为您不应该提供分数格式的输入。 Instead of using (9/4) format input just go with 2.25.而不是使用(9/4)格式输入,只需 go 和 2.25。

The reason that you are getting mismatch input error is your input is not valid in programming, in this case, you have to represent fraction numbers with a point like so: 9/4 => 2.25您收到不匹配输入错误的原因是您的输入在编程中无效,在这种情况下,您必须用这样的点表示分数:9/4 => 2.25

So the exception you are getting is java.util.InputMismatchException this is basically telling you that the input by the user doesn't match the expected input, which is what @SuppaGonzalo has also noted.因此,您得到的异常是java.util.InputMismatchException这基本上是在告诉您用户的输入与预期的输入不匹配, @SuppaGonzalo也注意到了这一点。

You can easily find the location of the error by looking at the stacktrace which appears below the exception:您可以通过查看出现在异常下方的堆栈跟踪轻松找到错误的位置:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:876)
at java.util.Scanner.next(Scanner.java:1502)
at java.util.Scanner.nextDouble(Scanner.java:2433)
at com.mathfunctions.mathfunctions.main(mathfunctions.java:8) <-- THIS LINE IS IMPORTANT
at java.lang.reflect.Method.invoke(Native Method)
at com.duy.android.compiler.java.Java.run(Java.java:115)
at com.duy.ide.javaide.run.activities.ExecuteActivity.executeDex(ExecuteActivity.java:147)
at com.duy.ide.javaide.run.activities.ExecuteActivity.exec(ExecuteActivity.java:124)
at com.duy.ide.javaide.run.activities.ExecuteActivity.access$100(ExecuteActivity.java:45)
at com.duy.ide.javaide.run.activities.ExecuteActivity$1.run(ExecuteActivity.java:88)
at java.lang.Thread.run(Thread.java:764)

As you can see in the stacktrace you are calling nextDouble on line 8 of mathFunctions class.正如您在堆栈跟踪中看到的,您在mathFunctions class 的第 8 行调用nextDouble

at java.util.Scanner.nextDouble(Scanner.java:2433) <-- THE METHOD WE'RE CALLING.
at com.mathfunctions.mathfunctions.main(mathfunctions.java:8) <-- THE LINE WE ARE CALLING NEXTDOUBLE ON.

You have successfully entered nextDouble which is fine, but the input the user has entered is NOT a Double, so InputMismatchException is thrown.您已成功输入nextDouble ,这很好,但用户输入的输入不是Double,因此会引发InputMismatchException

If you look at the code you have provided you have nothing on line 8 , that is because you haven't shared the whole code, you seem to have missed of the package part;如果您查看您提供的代码,您在第 8 行没有任何内容,那是因为您没有共享整个代码,您似乎错过了 package 部分; which if you add that in your error is happening this line: Double y2 = coordinates.nextDouble();如果您在错误中添加它,则会发生这一行: Double y2 = coordinates.nextDouble(); . .

From what you have said, you have entered a fraction of 9/4 , this is not a Double , so will not get parsed with nextDouble() .根据您所说,您输入了9/4的分数,这不是Double ,因此不会被nextDouble()解析。

What you probably want is something like this to convert your fraction to a Double .您可能想要的是将分数转换为Double的类似方法。

public static Double convertFractionToDouble(String fraction) throws ParseException {
    if (fraction != null) {
        if (fraction.contains("/")) {
            String[] numbers = fraction.split("/");
            if (numbers.length == 2) {
                BigDecimal numerator = BigDecimal.valueOf(Double.valueOf(numbers[0]));
                BigDecimal denominator = BigDecimal.valueOf(Double.valueOf(numbers[1]));
                BigDecimal response = numerator.divide(denominator, MathContext.DECIMAL128);
                return response.doubleValue();
            }
        } else {
            return Double.valueOf(fraction);
        }
    }
    throw new ParseException(fraction, 0);
}

and call it like:并称之为:

Double y2 = convertFractionToDouble(coordinates.next());

I would probably add some more validation to the convertFractionToDouble method but that is the general idea of what you want.我可能会向convertFractionToDouble方法添加更多验证,但这是您想要的一般想法。

Scanner.nextDouble() doesn't evaluate expressions: it just reads a literal double . Scanner.nextDouble()不计算表达式:它只是读取一个文字double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM