由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 一个比较菜鸟的数据输入的验证问题..谢谢..
相关主题
Eclipse不自动prompt methods咋搞定?java这个是什么逻辑?
请教 java 编程上的一个问题问个primitive type的问题
请教大家:如何modify java code in order to compile大虾们能帮我看看这个程序问题出在哪么?
很简单的代码,问题求助。太迷惑了问个autoboxing的问题
How to check if an element is in an array?请教一个多线程的问题
what is the problem?新手请教一个问题
Java练习题 2No decent way for input password from command line.
问个hashtable实现问题How to set a permanent path for windows ME
相关话题的讨论汇总
话题: input话题: scanner话题: finish话题: public话题: 输入
进入Java版参与讨论
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){
g*****g
发帖数: 34805
2
int a = 0;
while(true){
try{
a = scanner.nextInt();
if(! a in range) {
continue;
}else {
break;
}
} catch(Exception e){
continue;
}
}

【在 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){

o****i
发帖数: 1706
3
我改了下,新的代码是
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){
try{
System.out.println("Input an integer between 0 and 100:"
);
i=input.nextInt();
if(i>100||i<0){
System.out.println("Number input need to between 0
and 100");
}
h*****0
发帖数: 4889
4
因为你逻辑上不严谨。
输入错误了,你知道要finish = false;那输入正确时,你应该要记得让finish = true
;啊

【在 o****i 的大作中提到】
: 我改了下,新的代码是
: 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){
: try{
: System.out.println("Input an integer between 0 and 100:"

1 (共1页)
进入Java版参与讨论
相关主题
How to set a permanent path for windows MEHow to check if an element is in an array?
a question regarding appletwhat is the problem?
Question about TomcatJava练习题 2
html form and java问个hashtable实现问题
Eclipse不自动prompt methods咋搞定?java这个是什么逻辑?
请教 java 编程上的一个问题问个primitive type的问题
请教大家:如何modify java code in order to compile大虾们能帮我看看这个程序问题出在哪么?
很简单的代码,问题求助。太迷惑了问个autoboxing的问题
相关话题的讨论汇总
话题: input话题: scanner话题: finish话题: public话题: 输入