public class Solution {
public static void main(String args[])
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++){
int input1=sc.nextInt();
sc.nextLine();
String str1=sc.nextLine();
Double input2=sc.nextDouble();
}
}
}这是我的密码。我在"int“和"double”输入中只在for循环中获得了此异常。如果我把它写在for循环的外面,它就能正常工作。请任何人都能帮我解决这个问题,因为我在TCS考试中遇到了这个错误。
发布于 2022-02-23 10:32:52
不幸的是,你的问题并没有给我多少信息,但我会尝试回答无论如何(需要那个代表)。我假设你遇到的问题是一个InputMismatchException?这可能与您试图将double (即12.0)传递到int有关。使用类似System.out.println("Enter Int Input:")的内容可以帮助您突出显示应该在行中提供的输入类型。try/catch块可以停止抛出错误。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
for (int i = 0; i < 5; i++) {
System.out.println("i: " + i);
System.out.println("Enter Int Value:");
int input1 = sc.nextInt();
sc.nextLine();
System.out.println("Enter String Value:");
String str1 = sc.nextLine();
System.out.println("Enter Double Value:");
Double input2 = sc.nextDouble();
}
} catch (InputMismatchException e) {
System.out.println("Invalid input entered for variable type.");
}
}https://stackoverflow.com/questions/71234910
复制相似问题