zoukankan      html  css  js  c++  java
  • 多态与异常处理——动手动脑

    题目:

    编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

    要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

    源代码

     1 import javax.swing.JOptionPane;
     2 
     3 public class Test {
     4     public static String  judge(int s)
     5     {
     6         String output = null;
     7         if(s < 60)
     8             output = "不及格";
     9         else if(s < 70)
    10             output = "及格";
    11         else if(s < 80)
    12             output = "";
    13         else if(s < 90)
    14             output = "";
    15         else if(s <= 100)
    16             output = "";
    17         
    18         return output;
    19     }
    20     public static void main(String args[])
    21     {    
    22         while(true)
    23         { 
    24             try
    25             {
    26                 String input =JOptionPane.showInputDialog("请输入:"); //有可能引发运行时异常
    27                 int score = Integer.parseInt(input);
    28         
    29                 if(score<0||score>100)
    30                     throw new NumberFormatException();                      
    31             
    32                 JOptionPane.showMessageDialog(null,"成绩"+score+"
    "+judge(score),"结果",
    33                         JOptionPane.PLAIN_MESSAGE);
    34                 System.exit(0);
    35             }
    36             catch(NumberFormatException e) 
    37             {     
    38                 JOptionPane.showMessageDialog(null,"输入错误","警告!",
    39                         JOptionPane.ERROR_MESSAGE);
    40             }
    41         }
    42     }
    43 
    44 }

    结果截图

  • 相关阅读:
    svn上传文件钩子
    linux服务器版svn安装
    csp-s模拟55
    csp-s模拟54
    csp-s模拟53
    csp-s模拟52
    csp-s模拟51
    csp-s模拟50
    csp-s模拟49
    csp-s模拟48
  • 原文地址:https://www.cnblogs.com/weipinggong/p/4962540.html
Copyright © 2011-2022 走看看