07动手动脑

 编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃

源代码:

import java.util.Scanner; public class StudentScore {

 public static void main(String[] args) {   // TODO Auto-generated method stub   Scanner s = new Scanner(System.in);   System.out.println("请输入一个学生的成绩:");   String score = s.nextLine();//输入成绩   int i;   //try catch 实现健壮性   try   {    i=Integer.parseInt(score);//转换为int    //判断    if(i>=0&&i<60)    {     System.out.println("不及格");    }    else if(i>60&&i<70)    {     System.out.println("及格");    }    else if(i>70&&i<80)    {     System.out.println("中");    }    else if(i>80&&i<90)    {     System.out.println("良");    }    else if(i>90&&i<=100)    {     System.out.println("优");    }    else if(i>100||i<0)    {     System.out.println("输入有误");    }   }   catch (Exception a)   {    System.out.println("输入有误");   }  }

}

运行截图:

1、空格时

2、其他时

原文地址:https://www.cnblogs.com/hzxsg0919/p/4966666.html