我对发生了什么有疑问,每当我尝试编译它时,它总是给我一个像这样的错误:
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.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Person.main(Person.java:38)
我想要的只是让用户能够输入他们的年龄和姓名并将其存储在“年龄”和“姓名”变量中,然后让它在底部字符串中打印出来。如果有人也想帮我清理我的代码,那也没什么坏处。
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Person
{
public static void main(String[]args)
{
int age;
int name;
Scanner scan = new Scanner(System.in);
System.out.println("Enter in your age.");
age = scan.nextInt();
if (age < 18)
{
System.out.println("So you're a kid, huh? That's fine.");
}
else if (age >= 18)
{
System.out.println("Ah, and adult! Good.");
}
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.println("Enter in your name");
name = in.nextInt();
System.out.println("So you're " + age + " years old and your name is " + name);
}
}