package test; import javax.swing.JOptionPane; public class Shuru { double num; public void input(){ try{ String str=JOptionPane.showInputDialog("请输入成绩:"); num=Double.parseDouble(str); } catch(NumberFormatException e){ JOptionPane.showMessageDialog(null, "输入的不是数字!",null, JOptionPane.PLAIN_MESSAGE); input(); } finally{ JOptionPane.showMessageDialog(null, "ok!",null, JOptionPane.PLAIN_MESSAGE); } } public static void main(String args[]){ Shuru a=new Shuru(); while(true){ a.input(); if(a.num<=100&&a.num>=90){ JOptionPane.showMessageDialog(null, "成绩优秀!",null, JOptionPane.PLAIN_MESSAGE);break; } else if(a.num<=89&&a.num>=80){ JOptionPane.showMessageDialog(null, "成绩良好!",null, JOptionPane.PLAIN_MESSAGE);break; } else if(a.num<=79&&a.num>=70){ JOptionPane.showMessageDialog(null, "成绩中等!",null, JOptionPane.PLAIN_MESSAGE);break; } else if(a.num<=69&&a.num>=60){ JOptionPane.showMessageDialog(null, "成绩及格!",null, JOptionPane.PLAIN_MESSAGE);break; } else if(a.num<=59&&a.num>=0){ JOptionPane.showMessageDialog(null, "成绩不及格!",null, JOptionPane.PLAIN_MESSAGE);break; } else{ JOptionPane.showMessageDialog(null, "输入数字超出范围!",null, JOptionPane.PLAIN_MESSAGE); } } } }
设计思想:定义input方法,输入判断看看是不是数字,如果不是则递归输入,进入主函数后,while循环判断其是不是超出输入范围,并且判断成绩类型。
程序流程图:
原文:http://www.cnblogs.com/sisi-job/p/4964515.html