import java.util.Scanner;
public class Name {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String userName;
int userAge;
System.out.println("What is your full name?");
userName = in.nextLine();
System.out.println("What is your age?");
userAge = in.nextLine();
System.out.println("You're " + userName + " and you are " + userAge + " years old");
}
}
这个错误不断弹出,我找不到错误。请帮忙。我是一个新手,我已经对这个问题进行了研究。我找不到问题,我已经花了一周的时间来研究这个代码。我想把我的头发扯下来!!
最佳答案
userAge
是一个 int
,您使用 in.nextLine()
为其分配一个值。 Scanner#nextLine
返回一个 String
,它与 int
不兼容。使用Scanner#nextInt
代替:
userAge = in.nextInt();
in.nextLine();
关于java - 错误:incomparable types: String cannot be converted into int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430785/