此代码不执行任何循环(while(),for()) [英] This code do not executed any loop ( while() , for() )

查看:114
本文介绍了此代码不执行任何循环(while(),for())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Java ME问题.我正在使用Netbeans 7.0.设置为CLDC 1.1,MIDP 2.0,DefaultCldcJtwiPhone1,我的手机是Nokia 5130 Express Music.我做了这段代码,没有任何循环.这段代码仍然无法处理任何循环,并且在调用Random函数时,它们都显示相同的数字(除了在我的单元格中,"lotoMax"显示了不同的数字,但显示的不是"6/49".继续,请尝试此操作代码,然后选择" 6/49或lotoMax,放置一个循环,然后重试,仍然无法正常工作.代码将编译并运行,但运行不正常.我知道我在这里是NooB,我需要一些帮助,告诉我为什么循环不执行并且数字不能正确显示.我在这里感到沮丧.

我的问题:为什么它不起作用,如何解决它以及任何帮助.

此处的代码:

This is a Java ME question. I am using Netbeans 7.0. The setting is CLDC 1.1, MIDP 2.0, DefaultCldcJtwiPhone1, my cell is a Nokia 5130 Express Music. I did this code without any loops. Still this code not working with any loops, AND when the Random function is call, all display the same numbers ( except in my cell, the "lotoMax" show different numbers but not the "6/49". Go ahead, try it this code, and "select" 6/49 or lotoMax, place a loop and try again, still not working right. the code will compile & run , BUT not run well. I know I am a NooB here, I need some help and tell me Why the loop is not executed and the numbers not display right. I getting frustrated here.

My question : Why it did not work, How can I fix it and any help.

Here the code :

package Lotteryselect;

import javax.microedition.lcdui.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.microedition.midlet.*;

/**
 * @author Serge J Desjardins
 */
public class LotteryselectMidlet extends MIDlet implements CommandListener 
{
      // display manager
    private Display display,display649,displaylotomax;
    
    // a menu with items
    private List menu; // main menu

    // textbox
    private TextBox input;
    
    // Form
     private Form form649,formlotomax;
    
    // variables
    int set649,setlotomax;
    int bal1,bal2,bal3,bal4,bal5,bal6;
    
    

    // command
    static final Command backCommand = new Command("Back", Command.BACK, 0);
    static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1);
    static final Command exitCommand = new Command("Exit", Command.STOP, 2);
    private String currentMenu;

    /*
     * Start the MIDlet by creating a list of items and associating the
     * exit command with it.
     */
    public void startApp() throws MIDletStateChangeException  

    {
    
        
      display = Display.getDisplay(this);

      menu = new List("Lottery Selection", Choice.IMPLICIT);
      menu.append("Lotto 6/49", null);
      menu.append("Lotto Max", null);
      menu.append("Item3", null);
      menu.append("Item4", null);
      menu.addCommand(exitCommand);
      menu.setCommandListener(this);

      mainMenu();    
    }
    
    public void pauseApp()
    {
        
    }
    
    public void destroyApp(boolean unconditional)
    {
       notifyDestroyed();  
    }
    
    // main menu
    void mainMenu() {
      display.setCurrent(menu);
      currentMenu = "Main"; 
    }

    /**
     * a generic method that will be called when selected any of
     * the items on the list.
     */
    public void prepare() {
       input = new TextBox("Enter some text: ", "", 5, TextField.ANY);
       input.addCommand(backCommand);
       input.setCommandListener(this);
       input.setString("");
       display.setCurrent(input);
    }

    /**
     * Test item1.
     */
    public void lotto649 ()
    { 
      
      int a,b,c,d,e,f,i,count;
      
      // int[] bal649 = new int[49];
      
      /*
      count=1;
      for (i=0;i>49;i++)
      {
        bal649[i]=count;
        count++;
      }
       */
   

      int[] bal649 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
                      21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
                      38,39,40,41,42,43,44,45,46,47,48,49};
      
      a = new Random().nextInt(49);
      bal1=bal649[a];
       b = new Random().nextInt(49);
      bal2=bal649[b];
       c = new Random().nextInt(49);
      bal3=bal649[c];
       d = new Random().nextInt(49);
      bal4=bal649[d];
       e = new Random().nextInt(49);
      bal5=bal649[e];
       f = new Random().nextInt(49);
      bal6=bal649[f];
       
      form649 = new Form("6/49 Numbers");
      form649.append("\n\n\n");
      form649.append(bal1+" "+bal2+" "+bal3+" "+bal4+" "+bal5+" "+bal6);
           
      display649 = Display.getDisplay(this);
      display649.setCurrent(form649);
      form649.addCommand(backCommand);
      form649.setCommandListener(this);
           
      currentMenu = "Lotto 6/49";
    }
   
    /**
     * Test item2.
     */
    public void lottomax ()
    {
       
       formlotomax = new Form("Loto Max Numbers");
       formlotomax.append("\n\n\n");
       /*
       for (int i=0;i>6;i++)
       {
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotamax+" ";
       }
       */

         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         
      displaylotomax = Display.getDisplay(this);
      displaylotomax.setCurrent(formlotomax);
      formlotomax.addCommand(backCommand);
      formlotomax.setCommandListener(this);
      
      currentMenu = "Lotto Max"; 
    }

    /**
     * Test item3.
     */
    public void testItem3()
    {
       prepare();
       currentMenu = "item3"; 
    }

    /**
     * Test item4.
     */
    public void testItem4()
    {
       prepare();
       currentMenu = "item4"; 
    }
    
   /**
    * Handle events.
    */  
   public void commandAction(Command c, Displayable d)
   {
      String label = c.getLabel();
      if (label.equals("Exit")) {
         destroyApp(true);
      } else if (label.equals("Back")) {
          if(currentMenu.equals("Lotto 6/49") || currentMenu.equals("Lotto Max") ||
             currentMenu.equals("item3") || currentMenu.equals("item4"))  {
            // go back to menu
            mainMenu();
          } 

      } else {
         List down = (List)display.getCurrent();
         switch(down.getSelectedIndex()) {
           case 0: lotto649 ();break;
           case 1: lottomax ();break;
           case 2: testItem3();break;
           case 3: testItem4();break;
         }
            
      }
  }
}

推荐答案

Hm.以前从未见过这样的技巧.您弄乱了所有循环以执行零次.

看看你想做什么:
Hm. Never saw such artistry before. You messed up all your loops to execute zero times.

Look what you tried to do:
for (i=0; i>49; i++) {...}



应该是:



Should be:

for (i=0; i<49; i++) {...}



现在循环的主体将执行48次.

顺便说一句,"49"被称为立即常量-非常不被支持.想象一下如果您需要更改所有代码中的所有数字会发生什么.如果不能使所有事物都可变,则使它们成为常量,但要使其显式.

现在,您还有while循环吗?我的想象力无法猜测您会如何解决它们,也许类似于for循环.

—SA



Now the body of the loop will be executed 48 times.

By the way, "49" is called immediate constant — very unsupportable. Imagine what happens if you need to change all numbers in all code. If you cannot make all things variable, makes them constants but explicit.

Now, you also had while loop? My imagination fails to guess what could you screw up with them, maybe something similar to for loop.

—SA


添加到SA的答案中,最大的罪行是每次需要随机数时都会创建一个新的随机对象.
在J2ME CLDC1.1中:
Adding to SA''s answer, your biggest crime is that you create a new random object every time you want a random number.
In J2ME CLDC1.1:
new Random().nextInt(48);
//is the same as
new Random(System.currentTimeMillis()).nextInt(48);


因此,您将获得一个具有相同种子的随机类(因为NetBeans的模拟器针对此类操作的运行速度非常快),结果也相同.在您的手机中,该操作以较慢的方式进行(与图形操作相反,该操作在手机中运行得更快),从而产生了不同的种子和不同的结果.

现在,把它牢记在心:不管您创建哪种应用程序,只创建一个一个 Random类并在整个应用程序中使用它即可.

有关随机数的更多信息,请参见: http://en.wikipedia.org/wiki/Pseudorandom_number_generator [


Hence, you got a random class with the same seed (since NetBeans''s simulator run incredibly fast for this kind of operation) resulting same results. Whilst in your phone, the operation is carried in a slower manner (Opposite of graphics operation which run faster in phone) resulting different seeds and different results.

Now, hammer this into your head: doesn''t matter what kind of application you make, create only one Random class and use it thorough the whole application.

More reading on random numbers: http://en.wikipedia.org/wiki/Pseudorandom_number_generator[^]


Just create a static public variable in your MIDlet, initialize in static constructor and use it every time you want a random number
Example code :

public class LotteryselectMidlet extends MIDlet implements CommandListener 
{
    public static Random rand;

    static {
        rand = new Random();
    }

    //Example in the same class
    public void startApp() throws MIDletStateChangeException  
    {
        int i = rand.nextInt(48);
        int j = rand.nextInt(48);
    }
}
//Example in another class (should be in different file since declared public)
public class AnotherClass{
    public void MethodRandom(){
        int k = LotteryselectMidlet.rand.nextInt(48);
    }
}


上面的代码应该给出随机结果.


The above code should give random results.


@SAKryukov

是的,我只是注意到我的新手错误" i> 49到i< 49.我希望事情就这么简单.抱歉,代码混乱.

@Firo Atrum Ventus

感谢您的见解和链接.看起来我不得不重新设计我的代码.问题是:如何编码以便选择6个不同的数字?
@SAKryukov

Yep, I just notice my "rooky mistake" the i>49 to i<49. I hope it that simple. Sorry for the messy code.

@Firo Atrum Ventus

Thank for your insight and the link. Look like I have to re-design my code. The question is : How can I code it so it pick 6 different numbers ?


这篇关于此代码不执行任何循环(while(),for())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆