猜数字游戏的实现

来源:互联网 发布:网络舆情试题 编辑:程序博客网 时间:2024/05/17 09:04

 

package csdn.zuoye.com;

 

import java.util.Random;

 

import javax.swing.JOptionPane;

 

public class GuessNumDemo {

 static  String source="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//定义字符串

 static String message;//dingyi

 public static void main(String[] args) {

        //随机产生数字 创建Random的对象

Random random=new Random();

//调用nextInt方法返回一个0---source.length()-1

int index =random.nextInt(source.length());

//获得了随机产生的字母

char guessChar=source.charAt(index);

/*

//JOptionPane.showInputDialog(null, ""+guessChar);

for(int i=0;i<5;i++){

String inputChar=JOptionPane.showInputDialog("请输入你猜的数字");

    if(inputChar.equals(guessChar+"")){

     JOptionPane.showMessageDialog(null, "恭喜你 ,答对了");

    }else {

     JOptionPane.showMessageDialog(null, "对不起,你猜错了");

    

     //JOptionPane.showMessageDialog(null, "您猜大了")

    }

    */

if(guessChar>=48&&guessChar<=57){

message="请输入数字";

}else if(guessChar>=65&&guessChar<=90){

message="请输入大写字母";

}else if(guessChar>=97&&guessChar<=122){

message="请输入小写字母";

}

System.out.println(guessChar);

for(int i=0;i<5;i++){

String temp=JOptionPane.showInputDialog(message); 

char t=temp.charAt(0);

if(guessChar==t){

JOptionPane.showMessageDialog(null, "恭喜你答对了");

break;

}else if(guessChar>t){

JOptionPane.showMessageDialog(null,"你猜小了");

}else{

JOptionPane.showMessageDialog(null,"你猜大了");

}

}

}

}

 

 

 

原创粉丝点击