1.推荐 呕心沥血,自己写的!
public class Hello {
public static void main(String[] args) {
inputScore();
}
public static void inputScore(){
while(true){
try {
System.out.println("请输入您的分数:");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String score = bf.readLine();
String regex = "^[0-9]\\d*$";
if(score.equalsIgnoreCase("quit")){
System.out.println("谢谢你的使用,欢迎下次光临.");
System.exit(0);
}
if(score.matches(regex)){
getGrade(Integer.parseInt(score));
}
System.out.println("如果想退出输入,请输入quit");
} catch (Exception e) {
System.out.println("2222");
throw new RuntimeException("您的输入有误...");
}
}
}
public static void getGrade(int score){
if(score >= 90){
System.out.println("优秀");
}else if(score >= 80){
System.out.println("良好");
}else if(score >= 70){
System.out.println("一般");
}else if(score >= 60){
System.out.println("差强人意");
}else if( score >= 0){
System.out.println("补考");
}else{
System.out.println("您的输入有误,请重新输入:");
}
}
}
2. 普通的单次输入
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while(flag){
try {
System.out.println("请输入您的分数:");
int score = scanner.nextInt();
getGrade(score);
} catch (Exception e) {
System.out.println("分数必须是数字! 请重新输入:");
flag = false;
}
}
}
public static void getGrade(int score){
if(score >= 90){
System.out.println("优秀");
}else if(score >= 80){
System.out.println("良好");
}else if(score >= 70){
System.out.println("一般");
}else if(score >= 60){
System.out.println("差强人意");
}else if( score >= 0){
System.out.println("补考");
}else{
System.out.println("您的输入有误,请重新输入:");
}
}
}
原文:http://www.cnblogs.com/bravolove/p/5797535.html