【问题标题】:How to generate random numbers and distribute them randomly to some buttons?如何生成随机数并将它们随机分配到某些按钮?
【发布时间】:2017-04-17 07:29:46
【问题描述】:

我是android编程的菜鸟,想在特定范围内随机生成一些数字。假设其中两个之和等于某个数,我在onCreate()onRestart 中写了以下内容,以便在每次活动开始时重新生成它们,但并非每次都有效:

//i1,i2,i3..... are initialized in the class body as integers

      do {
        i1=r.nextInt((30-1)+1)+1;
        i2=r.nextInt((30-1)+1)+1;
        i3=r.nextInt((30-1)+1)+1;
        i4=r.nextInt((30-1)+1)+1;
        i5=r.nextInt((30-1)+1)+1;
        i6=r.nextInt((30-1)+1)+1;
        i7=r.nextInt((30-1)+1)+1;
        i8=r.nextInt((30-1)+1)+1;
        i9=r.nextInt((30-1)+1)+1;
    }  while (i1+i2!=25);

    String[] str= {String.valueOf(i1),String.valueOf(i2),String.valueOf(i3),
            String.valueOf(i4),String.valueOf(i5),String.valueOf(i6),
            String.valueOf(i7),String.valueOf(i8),String.valueOf(i9)};

    btn1= (Button) findViewById(R.id.btn1);
    btn2= (Button) findViewById(R.id.btn2);
    btn3= (Button) findViewById(R.id.btn3);
    btn4= (Button) findViewById(R.id.btn4);
    btn5= (Button) findViewById(R.id.btn5);
    btn6= (Button) findViewById(R.id.btn6);
    btn7= (Button) findViewById(R.id.btn7);
    btn8= (Button) findViewById(R.id.btn8);
    btn9= (Button) findViewById(R.id.btn9);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
    btn6.setOnClickListener(this);
    btn7.setOnClickListener(this);
    btn8.setOnClickListener(this);
    btn9.setOnClickListener(this);

    // this is the code which distribute the numbers randomly to the buttons:
    btn1.setText(str[random.nextInt(str.length)]);
    btn2.setText(str[random.nextInt(str.length)]);
    btn3.setText(str[random.nextInt(str.length)]);
    btn4.setText(str[random.nextInt(str.length)]);
    btn5.setText(str[random.nextInt(str.length)]);
    btn6.setText(str[random.nextInt(str.length)]);
    btn7.setText(str[random.nextInt(str.length)]);
    btn8.setText(str[random.nextInt(str.length)]);
    btn9.setText(str[random.nextInt(str.length)]);

有时确实有两个数字的总和为“25”,但有时没有,我不明白原因。 而且,我想知道如何在不重复的情况下生成这些数字。

【问题讨论】:

  • 可以动态创建按钮吗?
  • 你能解释更多吗? @RushiAyyappa

标签: java android random


【解决方案1】:

在您的 activity.xml 中:获取一个 linearLayout 并将其标识为 ll_main。

在你的 onCreate() 中

       ArrayList<Integer> al_list=new ArrayList<Integer>();
          //generate random number
         ll.removeAllViews();
          for(int i=0;i<10;i++)
              {

                  int num=getRandomNumberInRange(0,10);
                  if(num!=-1){
                 al_list.add(num); 
                 Button btn=new Button(this);
                 btn.setId(num);
                 btn.setText(num+"");
                 ll_main.addView(btn);                      
                  }


              }

    private  int getRandomNumberInRange(int min, int max) {

          if (min >= max) {
        throw new IllegalArgumentException("max must be greater than min");
    }

    Random r = new Random();
int num=r.nextInt((max - min) + 1) + min;
     if(al_list.contains(num))
       {
        getRandomNumberInRange(0,10);
     return -1;
        }
      else{
      return num;
      }  
}

【讨论】:

  • 但这并没有给出总和为 "25" 的两个数字或任何其他数字,
  • 请更改随机数字逻辑..但按钮逻辑将起作用
  • 这至少会给出两个数字,它们的总和等于 = ... ??
【解决方案2】:

你可以像这样使用Random class

  final int total=25;
  int otherno;
  Random num =new Random();
    int mo=num.nextInt(24);
  otherno= total-mo;

这将生成两个 no mootherno,总和等于 25,如果您想要多对,只需将其放入 for 循环,它将随机生成。我使用 final 关键字,因为总数不是将改变任何没有。 for more info,一共random no ex

【讨论】:

  • 很好的解决方案,但我认为他会想使用 num.nextInt(25) 来获取 0 到 25 之间的数字
  • 查看更新答案@BrianPipa 和 Mohammad Abubeih
猜你喜欢
  • 2023-04-08
  • 2018-09-19
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多