Java 为什么在这段代码中出现输入不匹配异常?
我只是想从键盘上读取整数、双精度、字符串值。Java 为什么在这段代码中出现输入不匹配异常?,java,string,Java,String,我只是想从键盘上读取整数、双精度、字符串值。 它只适用于整数和双精度。但说到字符串,它抛出了一个输入不匹配异常,所以我无法从键盘读取字符串 import java.util.Scanner; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i =
它只适用于整数和双精度。但说到字符串,它抛出了一个输入不匹配异常,所以我无法从键盘读取字符串
import java.util.Scanner;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d= scan.nextInt();
String s=scan.nextLine();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
首先,你要扫描一张双人照片。这不是最好的办法。第二,尝试scan.next以获取任意类型的令牌
int i = scan.nextInt();
double d= scan.nextDouble();
String s=scan.next();
你用过
双d=scan.nextInt
改用
双d=scan.nextDouble
此外,字符串对于以下输入和输出工作正常:
你好
字符串:你好
双倍:1.0
Int:1请检查下面的代码。它对我有用
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("enter i value ");
int i = scan.nextInt();
System.out.println("enter d value ");
double d= scan.nextDouble();
System.out.println("enter s value ");
String s=scan.next();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
我认为字符串s=scan.nextLine;使用来自以下调用double d=scan.nextInt;的换行符
这是我的两分钱
double d = scan.nextInt();
scan.nextLine();
String s = scan.nextLine();
字符串s=scan.next@user158302您能告诉我为什么要单独导入Scanner类吗?但是当涉及到字符串时,它抛出了一个输入不匹配异常,这是非常令人难以置信的,因为字符串可以表示用户提供的任何字符,所以它不应该不匹配。另一个问题是nextLine很可能会返回空字符串,请参阅以了解更多详细信息。无论如何,为了获得正确的答案,我们需要对您的问题提供正确的答案,并提供正确的答案。除了代码之外,还应包括您在运行代码时使用的输入以及您收到的确切错误消息。我认为这与对象类型无关,因为在线程主java.util.InputMismatchException中,输入的结果自动升级为double.1.0 Hello异常