Java双色球机选程序(使用TextBox)

import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

import javax.microedition.lcdui.Command; 

import javax.microedition.lcdui.CommandListener; 

import javax.microedition.lcdui.Display; 

import javax.microedition.lcdui.Displayable; 

import javax.microedition.lcdui.TextBox; 

public class ShuangSeQiu extends MIDlet implements CommandListener  {

private TextBox textbox; // Textbox 显示一条消息 

private Display disp; // 引用MIDletDisplay 对象 

private Command cmdExit; // 设定按钮用于退出MIDlet 

private Command cmdOK; // 确定按钮 

private MyCalc myCalc;

public ShuangSeQiu() {

super();

//---

disp = Display.getDisplay(this); // 获得当前MIDletDisplay对象,以便进行界面显示

//---

textbox = new TextBox("双色球机选程序", "", 40, 0); // 新建文本框 

//---

cmdOK = new Command("生成", Command.OK, 1); 

cmdExit = new Command("退出", Command.EXIT, 1); // 新建两个控制按钮 

//---

textbox.addCommand(cmdOK); 

textbox.addCommand(cmdExit); // 添加控制按钮 

textbox.setCommandListener(this); // 开始侦听命令 

//---

myCalc = new MyCalc();

}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

// TODO Auto-generated method stub

}

protected void pauseApp() {

// TODO Auto-generated method stub

}

protected void startApp(){

disp.setCurrent(textbox);

}

public void commandAction(Command arg0, Displayable arg1)  

if (arg0 == cmdExit) { // 按下退出键时停止运行 

try {

destroyApp(false);

} catch (MIDletStateChangeException e) {

// TODO Auto-generated catch block

e.printStackTrace();

notifyDestroyed(); 

} else if (arg0 == cmdOK) { // 按下阅读键后阅读对应信息 

textbox.setString(myCalc.getSSQ());

}

 

import java.util.Random;

public class MyCalc {

private final Random random_R = new Random();

private final Random random_B = new Random();

int[] items_R = new int[33];  

int item_B;  

    private String FormatEx(int v){

     if (v < 10){

     return "0" + v;

     }

     else{

     return "" + v;

     }    

    }  

    

    public String getSSQ() {

        //---初始化

     int itemCount = 0;

     int i;

        for (i = 1; i < items_R.length; i++) {  

         items_R[i] = 0; 

         itemCount++;  

        }  

        //---选择红球

        int counter = 0;  

        int id;

        int index;

        while (true) { 

         id = random_R.nextInt(itemCount);

         if (id == 0) {  

                continue;  

            }    

         //---

         index = 0;

            for (i = 1; i < items_R.length; i++) {  

             if (items_R[i] == 0)

             index++;  

             if (index == id){

             items_R[i] = 1;

             break;

             }

            }              

         itemCount--;

         //---

         counter++;  

            if (counter == 6)  

                break;        

        }

        //---选择蓝球

        while (true) {         

         item_B = random_B.nextInt(16);

         if (item_B != 0) 

         break;        

        }

        //---输出结果

        StringBuffer strResult = new StringBuffer(); 

        for (i = 1; i < items_R.length; i++) {  

         if (items_R[i] == 1){

                strResult.append(FormatEx(i));  

                strResult.append(",");  

         }

        }   

        strResult.deleteCharAt(strResult.length() - 1);

        //---

        strResult.append("+");       

        strResult.append(FormatEx(item_B));  

        //---        

        return strResult.toString();      

    }   

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值