用户在Java中输入字符串和整数 [英] user input string and integers in Java

查看:262
本文介绍了用户在Java中输入字符串和整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    System.out.print("Name : ");
    String name = in.nextLine();

    System.out.print("Age : ");
    int age = in.nextInt();

    System.out.print("City : ");
    String city = in.nextLine();

输出将为:

名称:test

年龄:20

建立成功

当我调试它们时,它不会读取city的用户输入。但是当我将age的数据类型更改为string时,它将会读取。如何通过系统读取城市的用户输入,将年龄的数据类型保持为int?

when i debug them, it won't read the user input for "city" . but when i changed the data type for "age" to string, it will read. how can i kept the data type of age to int with the system reading the user inputs for city ?

推荐答案

由于读取年龄后缓冲区中仍有新行字符,请将其更改为..

As there is still a new line character in the buffer after reading the age, change it to..

System.out.print("Name : ");
String name = in.nextLine();

System.out.print("Age : ");
int age = in.nextInt();
//add
in.nextLine();

System.out.print("City : ");
String city = in.nextLine();

这篇关于用户在Java中输入字符串和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆