猜數字遊戲的實現

 

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,"你猜大了");

}

}

}

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章