很抱歉提出一个简单的问题,但我在这里是一个初学者。所以问题是当我将以下代码复制到我的日食中时,它工作正常。但是当我将变量的类型从int更改为double时,它会显示某种错误。请检查一下。
import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
double x, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextDouble();
y = in.nextDouble();
z = x + y;
System.out.println("Sum of entered integers = "+z);
}
}
btw,错误如下
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at AddNumbers.main(AddNumbers.java:10)
这里是我刚看过的视频教程(https://www.youtube.com/watch?v=ANuuSFY2BbY)我刚试过复制它
import java.util.Scanner;
class HelloWorld{
public static void main(String args[]) {
Scanner bucky = new Scanner(System.in);
double fnum;
double snum;
double answer;
System.out.println("Enter first number:");
fnum = bucky.nextDouble();
System.out.println("Enter second number:");
snum = bucky.nextDouble();
answer = fnum+snum;
System.out.print(answer);
}
}
我收到以下错误:
Enter first number:
34.6
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at HelloWorld.main(HelloWorld.java:11)