【问题标题】:Looping solutions to While statements?While 语句的循环解决方案?
【发布时间】:2014-10-08 22:41:59
【问题描述】:

我有一个 while 语句,它产生一个介于 15 和 32 以及 212 和 229 之间的数字。

import java.util.Scanner;
import java.util.Random;
public class Test
{
public static void main(String[] args)

{
    int randNum = 0;
    Random randNumList = new Random();

    while( !(randNum>=15 && randNum<=32)&& !(randNum>=212 && randNum<=229))
     {
        randNum = randNumList.nextInt(251);
     }
     System.out.println(randNum);

我可以使用什么类型的循环来生成这些“randNum”的给定数量(例如 10)?例如,如果 while 语句生成数字 24,我如何从该 while 语句中生成 15/32 和 212/229 之间的 10 个数字?

【问题讨论】:

    标签: java random while-loop numbers


    【解决方案1】:

    虽然您创建随机数的方式可能不是最有效的——对代码的最小侵入性更改是简单地将您的 while 包装在 for 循环中……您可以将结果添加到数组或列出以便稍后访问它们。

    Random randNumList = new Random(); //do this only once - no need to re-create it you can re-use it
    
    for(int i=0; i<asManyAsYouWantPlusOne; i++){
    
           randNum = 0; //important! reset here or you will end up with all the same random numbers ...
    
           while( !(randNum>=15 && randNum<=32)&& !(randNum>=212 && randNum<=229))
           {
             randNum = randNumList.nextInt(251);
           }
          //add to list, or array here ...
           System.out.println(randNum);
         }
    

    【讨论】:

    • 你绝对不应该在外循环的每次迭代中创建new Random()
    • 我将此代码添加到程序中,但同样,只出现了一个数字。我的问题有点不清楚,我很抱歉。我想要得到的是一个包含 10 个不同数字的列表,所有这些数字都在 15/32 和 212/229 之间。不过感谢您的意见!
    • 非常感谢!我会投票,但我没有足够的声誉!
    【解决方案2】:

    如果我正确理解了您的建议,您可以简单地在其周围添加一个 for 循环,如下所示:

    for(int i = 0; i < 10; i++)
    {
        while( !(randNum>=15 && randNum<=32)&& !(randNum>=212 && randNum<=229))
        {
            randNum = randNumList.nextInt(251);
        }
        System.out.println(randNum);
    }
    

    但是,您的 while 循环是无效的,因为它会生成多个不必要的数字。您可以简单地要求一个较小范围内的数字(在 15 到 32 之间有 (32-15+1=) 18 个数字,在 212 到 229 之间还有其他 (229-212+1=) 18 个数字 - 总共 36 个数字,所以),并调整它,如:

    randNum = randNumList.nextInt(36)+15; // this creates numbers between 15 and 50
    if(randNum > 32) randNum += 179; // this transforms the numbers between 33 and 50
                                     // in numbers between 212 and 229
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      根据您对 Birgit Martinelle 的响应,您的实际目标似乎是保证指定范围内的一组唯一值。 (如果是这样,你应该在你的问题中而不是在 cmets 中这样说,因为那是一个完全不同的问题。)

      一个不错的解决方案是打乱集合并遍历结果以挑选出一个子集:

      ArrayList<Integer> al = new ArrayList<Integer>();
      for(int i = 15; i < 33; ++i) {
         al.add(i);
      }
      for(int i = 212; i < 230; ++i) {
         al.add(i);
      }
      
      // Repeat (loop over) the following lines as many times as you need subsets.
      Collections.shuffle(al);
      for(int i = 0; i < 10; i++) {
         System.out.println(al.get(i));
      }
      

      您当前的拒绝方案非常低效——您放弃了 216/251(超过 85%)的尝试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多