龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

猜数字游戏,我的第一个J2ME程序,很多问题,有待于解决

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
import Javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*; /** * * @authoraoenzh * @version */ public class Test extends MIDlet implements CommandListener{ private Command cmdOK; private Command cmdEXIT; priv

import Javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  import java.util.*;

/**
   *
   * @author  aoenzh
   * @version
   */
  public class Test extends MIDlet implements CommandListener{
     
      private Command cmdOK;
      private Command cmdEXIT;
      private TextField txtInput;
      private Form myForm;
      private int rndNum;
      //private int inputNum;
      Display display;
      public Test(){
         
      }
     
      public void startApp() {
          Random tmpRand=new Random();
          rndNum=tmpRand.nextInt(100);
          txtInput=new TextField("请输入0-50之间的数字","",25,TextField.NUMERIC);
          cmdOK=new Command("确认",Command.SCREEN,1);
          cmdEXIT=new Command("退出",Command.EXIT,1);
          myForm=new Form("猜数字");
          myForm.append(txtInput);
          myForm.addCommand(cmdOK);
          myForm.addCommand(cmdEXIT);
          myForm.setCommandListener(this);
          display =Display.getDisplay(this);
          display.setCurrent(myForm);
      }
     
      public void pauseApp() {
      }
     
      public void destroyApp(boolean unconditional) {
      }
     
      public void commandAction(Command command, Displayable displayable) {
          if(command==cmdEXIT){
              destroyApp(true);
              notifyDestroyed();
          }
          if(command==cmdOK){
              Alert alert = new Alert("猜数字","",null,AlertType.INFO);
              String input = txtInput.getString();
              int temp;
              if(input.length()>0){
                  temp = Integer.parseInt(input);
              } else{
                  temp=0;
              }
              alert.setTimeout(3000);
              if(temp > rndNum){
                  alert.setString("大了,笨蛋!");
                  Display.getDisplay(this).setCurrent(alert);
              }
              if(temp == rndNum){
                  alert.setString("佩服佩服!");
                  Display.getDisplay(this).setCurrent(alert);
                  //this.startApp();
              }
              if(temp < rndNum){
                  alert.setString("小了,笨蛋!");
                  Display.getDisplay(this).setCurrent(alert);
              }
              txtInput.setString("");
          }
      }
     
  }
  


  
精彩图集

赞助商链接