如何解决询问年龄后,扫描仪停止工作,并且在年龄问题后将不允许用户输入
询问年龄后,扫描仪将停止询问用户输入喜爱的甜点。我像在myObj的下一行上面一样跟随它,但是它仍然停止。请帮助
包裹扫描仪; 导入java.util.Scanner;
使用扫描器的公共类{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myObj = new Scanner(System.in);
String userName;
String dessert;
int age;
System.out.println("Hi What is your name? ");
userName = myObj.nextLine();
System.out.println ("How are you doing," + userName + "?");
System.out.println ("What is you age," + userName + "?");
age = myObj.nextInt();
//System.out.println ("Hello," + userName + " you are " + age);
System.out.println ("ok," + userName + " .Your are " + age + "." + " What is your favorite dessert?");
dessert = myObj.nextLine();
System.out.println (" Hello," + userName + " You are " + age + " and your favorite dessert is " + dessert);
}
}
解决方法
不要使用nextLine
,它会与所有其他nextX()
方法怪异地交互。相反,请更改扫描仪的定界符。制作完扫描仪后,调用:
scanner.useDelimiter("\r?\n");
然后得到例如一个整数,调用.nextInt()
。要获得某人的名字,请致电.next()
(不要为任何电话呼叫.nextLine()。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。