java里input错误,线程主java.util.InputMismatchException错误中的异常

I have a question on what's going on, whenever I try to compile it it keeps giving me an error like this:

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)

All I want is for the user to be able to input their age and name and have it stored in the "age" and "name" variables, then have it print it out in the bottom string. And if someone would like to help me clean up my code as well, it wouldn't hurt..

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);

}

}

解决方案

Issues

int name; //Name should be of type String

...

System.out.println("Enter in your name");

name = in.nextInt(); //It doesn't handle the string since your using `nextInt`

Solution

String name;

...

System.out.println("Enter in your name");

name = in.nextLine();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值