求大大用java编写一个小程序

要求如下当用户输入20以下的数字(int)的时候用户可以继续输入但是当输入20以上或者其它字符的时候显示error·直接贴代码谢谢,... 要求如下 当用户输入20以下的数字(int)的时候 用户可以继续输入 但是当输入20以上 或者其它字符的时候 显示error·

直接贴代码谢谢,
展开
 我来答
zhouxiaobinks
2010-11-03 · TA获得超过425个赞
知道小有建树答主
回答量:316
采纳率:0%
帮助的人:228万
展开全部
public class ReadDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Please input an integer(more than 20 or non-integer to end)");//提示输入

int inputValue = scanner.nextInt();

while(inputValue < 20){
System.out.println("Please input an integer(more than 20 or non-integer to end)");//小于20继续提示输入
try{
inputValue = scanner.nextInt();
}catch(InputMismatchException exp){
System.out.println("Error!Invalid value, program ends!");//类型出错,程序出错,停止哦
break;
}
if(inputValue >= 20 ){
System.out.println("Error! Invalid value, program ends!");//数字大于20,出错,程序停止额
break;
}
}
}
}
flyingFish211
2010-11-03 · TA获得超过2.1万个赞
知道大有可为答主
回答量:1.5万
采纳率:50%
帮助的人:1.1亿
展开全部
import java.util.InputMismatchException;
import java.util.Scanner;

public class ReadDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Please input an integer(more than 20 or non-integer to end)");//提示输入

int inputValue = scanner.nextInt();

while(inputValue < 20){
System.out.println("Please input an integer(more than 20 or non-integer to end)");//小于20继续提示输入
try{
inputValue = scanner.nextInt();
}catch(InputMismatchException exp){
System.out.println("Error!Invalid value, program ends!");//类型出错,程序出错,停止
break;
}

if(inputValue >= 20 ){
System.out.println("Error! Invalid value, program ends!");//数字大于20,出错,程序停止
break;
}
}

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
mwyuupl
2010-11-03 · TA获得超过170个赞
知道答主
回答量:75
采纳率:0%
帮助的人:57.4万
展开全部
import java.util.InputMismatchException;
import java.util.Scanner;

public class ReadDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("请输入数字:");//提示输入

int inputValue = scanner.nextInt();

while(inputValue < 20){
System.out.println("请继续输入数字:");//小于20继续提示输入
try{
inputValue = scanner.nextInt();
}catch(InputMismatchException exp){
System.out.println("Error!");//出错
break;
}

if(inputValue >= 20 ){
System.out.println("Error!");//数字大于20,出错,程序停止
break;
}
}

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
哭泣的大菜刀
2010-11-03 · TA获得超过154个赞
知道答主
回答量:242
采纳率:0%
帮助的人:0
展开全部
import java.io.*;
public class InputNum {
InputStreamReader ir;
BufferedReader br;
public static void main(String[] args) {
while(true){
try{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
String line=br.readLine();
int aline=Integer.parseInt(line);
if(aline<=20){
System.out.println(line);
}
else{
System.out.println("请出入有效数字!");
break;
}
}
catch(Exception e){
System.out.println("请出入有效数字!");
break;
}
}

}

}
//如果什么地方不严密再和我说
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式