由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 比较简单的Java数据输入验证问题
相关主题
Questions about arrow key in linux command prompthelp abt C++
面试题 -算法?C程序的异常输出
c++如何把小数转成二进制输出到文本文件?讨论个java作业问题
急!java 入门, 代码问题~~请各位好心人帮帮忙!What's the problem with 'ascii' codec? (转载)
java 8 也可以fp啊Anyone ever ran into a "fatal error LNK1168" in MS VS studi
gdb debugging the system call execve() ...请教如何修正这个C程序的bug。
How to change a string under gdb promptQt Creator
Please help: regarding ftp scripting in Windows xp Command prompt用pexpect写自动登录
相关话题的讨论汇总
话题: scanner话题: input话题: java话题: 输入话题: 小数
进入Programming版参与讨论
1 (共1页)
o****i
发帖数: 1706
1
题目是这样的:
Write a code segment to prompt the user to input an integer between 0 and
100 (inclusive) and keep prompting the user until he does so. Verify both
the integer data type entered and the range.
我写的代码,有两个问题,一个是如果第一次输入的是小数,那会进入异常处理,可是
如果在catch的时候继续输入小数,那JAVA就处理不了了,然后就会出错。怎么才能改
进达到要求呢?谢谢!
import java.util.*;
public class Input {
public static void main(String[] args){
Scanner input= new Scanner(System.in);
boolean finish=true;
int i=-1;
while(finish){
X****r
发帖数: 3557
2
你在catch里面不应该再开个新的Scanner,而是让原来的Scanner跳过剩下的部分,
比如一直跳过行尾,这样下次循环的时候原来的Scanner就可以继续工作了。

and
both

【在 o****i 的大作中提到】
: 题目是这样的:
: Write a code segment to prompt the user to input an integer between 0 and
: 100 (inclusive) and keep prompting the user until he does so. Verify both
: the integer data type entered and the range.
: 我写的代码,有两个问题,一个是如果第一次输入的是小数,那会进入异常处理,可是
: 如果在catch的时候继续输入小数,那JAVA就处理不了了,然后就会出错。怎么才能改
: 进达到要求呢?谢谢!
: import java.util.*;
: public class Input {
: public static void main(String[] args){

1 (共1页)
进入Programming版参与讨论
相关主题
用pexpect写自动登录java 8 也可以fp啊
Big Data’s Next Big Battle: IBM Versus… GE?gdb debugging the system call execve() ...
Re: how do you show this navigation bar in screen? (转载)How to change a string under gdb prompt
java小问题Please help: regarding ftp scripting in Windows xp Command prompt
Questions about arrow key in linux command prompthelp abt C++
面试题 -算法?C程序的异常输出
c++如何把小数转成二进制输出到文本文件?讨论个java作业问题
急!java 入门, 代码问题~~请各位好心人帮帮忙!What's the problem with 'ascii' codec? (转载)
相关话题的讨论汇总
话题: scanner话题: input话题: java话题: 输入话题: 小数