Java的新手,很难弄清为什么它不起作用,在获取用户输入的一半途中,它意外地停止了运行。我评论了它的坏处。错误消息:“线程“ main”中的异常java.util.InputMismatchException”
有人可以帮忙吗?
import java.util.Scanner;
public class BasicInput {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
//Initializations
int userInt = 0;
double userDouble = 0.0;
String userString = "";
char character = '?';
//Take User Input
System.out.println("Enter integer: ");
userInt = scnr.nextInt();
System.out.println("Enter double: ");
userDouble = scnr.nextInt();
/* ERROR OCCURS HERE */
System.out.println("Enter character: ");
character = scnr.next().charAt(0);
System.out.println("Enter string: ");
userString = scnr.next();
//Print User Input
System.out.println(userString);
System.out.print(character);
System.out.print(userDouble);
System.out.print(userInt);
//Cast double to an integer
int userInt2 = 0;
userInt2 = (int)userDouble;
System.out.print(userInt2);
return;
}
}
一只名叫tom的猫
慕斯709654
相关分类