Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:程序代码——带有验错功能的猜数字小游戏

1楼
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编辑过]
共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .04785 s, 2 queries.