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

2024-11-22 14:09:28
推荐回答(4个)
回答1:

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;
}
}
}
}

回答2:

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;
}
}

}
}

回答3:

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;
}
}

}
}

回答4:

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;
}
}

}

}
//如果什么地方不严密再和我说