以文本方式查看主题 - 课外天地 李树青 (http://www.njcie.com/bbs/index.asp) -- Java程序语言课件 (http://www.njcie.com/bbs/list.asp?boardid=17) ---- 程序代码——带有验错功能的猜数字小游戏 (http://www.njcie.com/bbs/dispbbs.asp?boardid=17&id=449) |
-- 作者:admin -- 发布时间:2008/3/17 8:35:26 -- 程序代码——带有验错功能的猜数字小游戏 public class exec { public static void main(String args[]) { //设置随机数 int rnd=(int)(Math.random()*100)+1; //设置统计次数 int counter=1; while(true) { String str=javax.swing.JOptionPane.showInputDialog("Please input the number:"); //判断是否没有输入或者输入为空,如果是则退出 if(str==null || str.equals("")) { javax.swing.JOptionPane.showMessageDialog(null,"No input!"); break; } //判断输入的内容是否不为数字,如果是则退出 char[] sarray =str.toCharArray(); for(int c=0;c<sarray.length;c++) if(!Character.isDigit(sarray[c])) { javax.swing.JOptionPane.showMessageDialog(null,"Error input!"); System.exit(0); } int i=Integer.parseInt(str); //三重判断 if(i<rnd) javax.swing.JOptionPane.showMessageDialog(null,"<"); else if(i>rnd) javax.swing.JOptionPane.showMessageDialog(null,">"); else { javax.swing.JOptionPane.showMessageDialog(null,"Succeed!\\nYou get it in "+ counter + " times!"); break; } counter++; } //退出主程序,关闭窗体资源 System.exit(0); } } [此贴子已经被作者于2010-12-12 08:23:17编辑过]
|