日期:2014-05-20 浏览次数:20945 次
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入1-100以内的数字");
String str = sc.nextLine();
if(str.matches("\\d*")){//判断是否是数字
int temp = Integer.parseInt(str);
if(temp>=1&&temp<=20){
System.out.println("E");
} else if(temp>=21&&temp<=40){
System.out.println("D");
} else if(temp>=41&&temp<=60){
System.out.println("C");
} else if(temp>=61&&temp<=80){
System.out.println("B");
} else if(temp>=81&&temp<=100){
System.out.println("A");
} else
System.err.println("您输入的数字不符合要求");
} else
System.err.println("请输入数字");
System.out.println("是否继续y/n");
String s = sc.nextLine();
if(s.equalsIgnoreCase("n")){
System.out.println("谢谢使用再见");
break;
}
}
}
------解决方案--------------------
楼上正解,补充
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("请输入1-100以内的数字");
try {
int score = sc.nextInt();
if (score <= 100 && score >= 1) {
System.out.println((char) ('E' - ((score - 1)) / 20));
} else {
System.out.println("您输入的数字超过范围");
}
} catch (Exception e) {
System.out.println("您输入的数字不符合要求");
}
sc.nextLine();
System.out.println("是否继续y/n");
String s = sc.next();
if (s.equals("y")){
flag = true;
}
else if(s.equals("n")){
System.out.println("谢谢使用再见");
break;
}
}
}
}
------解决方案--------------------
楼上正解。。考虑相当全面
------解决方案--------------------
我觉得还是用switch case吧做吧,但是我觉得被耍了。每次学计算机语言都会有相似的题目。如成绩的那个
这里拿出来问显得也太……
------解决方案--------------------
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("请输入一个1-100之间的整数:");
try {
int a = input.nextInt();
if (a >= 1 && a <= 20) {
System.out.println("E");
} else if (a >= 21 && a <= 40) {
System.out.println("D");
} else if (a >= 41 && a <= 60) {
System.out.println("C");
} else if (a >= 61 && a <= 80) {
System.out.println("B");
} else if (a >= 81 && a <= 100) {
System.out.println("A");
} else if (a < 1 || a > 100) {
System.out.println("输入有误,请输入一个1-100之间的整数");
}
System.out.println("是否继续?(Y/N)");
String i = input.next();
if ("N".equals(i)) {
System.out.println("拜拜!");
break;
}
} catch (InputMismatchException e) {
System.out.println("请输入数字!");
} finally {
input.nextLine();
}
}
}
}