如果boolean为false,如何使循环再次运行

时间:2014-10-30 07:31:29

标签: java loops methods

我的问题是当boolean为false时它仍然返回i并将其放在数组lotto中。我如何修复它,以便当布尔值为false时,它将丢弃i并为该元素运行另一个随机数。

package LottoNumbers;

import java.util.Arrays;

public class LottoNumbers {
//check for duplicates in each array

    public static boolean isFound(int[] lotto, int number) {
        for (int i = 0; i < lotto.length; i++) {
            if (lotto[i] == number ) {
                return true;

            }
        }
        return false;
          //DO SOMETHING IF FALSE THAT WILL GET RID OF THE NUMBER

    }

    public static void main(String[] args) {

        //specify length of array
        int[] lotto = new int[6];
        //determine how many arrays
        for (int Set = 1; Set <= 5; Set++) {
            //assign random numbers to each array element
            for (int i = 0; i < lotto.length; i++) {
                int number= 0;
                isFound(lotto, number = (int) (Math.random() * 50));
                lotto[i] = number;

            }
              Arrays.sort(lotto);


            //sort elements in array 
            //Sort arrays to specified Set numbers
            if (Set == 1) {
                System.out.printf("LOTTO Numbers for set 1 --> ");

            } else if (Set == 2) {
                System.out.print("LOTTO Numbers for set 2 --> ");

            } else if (Set == 3) {
                System.out.print("LOTTO Numbers for set 3 --> ");

            } else if (Set == 4) {
                System.out.print("LOTTO Numbers for set 4 --> ");

            } else if (Set == 5) {
                System.out.print("LOTTO Numbers for set 5 --> ");

            }

            System.out.printf(Arrays.toString(lotto).replace("[", "").replace(",", "").replace("]", "") + "\n");

        }
    }
}

2 个答案:

答案 0 :(得分:0)

我不完全确定你要做什么 - 你实际上根本没有使用isFound返回的布尔值做任何事情。但是,如果你问你是否只能在isFound评估为true时分配lotto [i],你可以这样做的一种方法是更改​​for循环,如下所示:

for (int i = 0; i < lotto.length; i++) {
    int number;
    Boolean wasFound = isFound(lotto, number = (int) (Math.random() * 50));

    if (!wasFound)
    {
        lotto[i] = number;
    }
    else
    {
        // perform the previous iteration again
        i--;
    }
}

您的设计不是非常有效或设计得很好 - 您依靠的是您之前未曾见过的随机数,这会使您已经创建的数字越多,性能就越差。你可以做的更好的方法是在你想要的范围内只取一个预先存在的非重复整数集合的单个元素:

// define numbersArrayList as { 1, 2, 3, 4, ... n }
ArrayList<int> yourLottoNumbers = new ArrayList<int>()
for (int i = 0; i < yourLimit; i++)
{
    int randomIndex = (int)(Math.random() * numbersArrayList.length()) - 1;
    int newNumber = numbersArrayList[randomIndex];
    yourLottoNumbers.add(newNumber);
    yourLottoNumbers.remove(randomIndex);
}

此代码的执行时间与您需要获取的元素数量成正比,而不是在完全随机的时间内,并且您不需要在重复循环时捏造。< / p>

答案 1 :(得分:0)

如果函数是假的,你不希望你的函数循环,因为那时函数永远不会结束。

public static boolean IsFound(int number)
{
    //You don't need to pass the array, because it's a class level variable
    for (int i = 0; i < lotto.length; i++)
    {
        if (lotto[i] == number)
            return true;
        return false;
    }
}

完成后,您只需执行以下操作即可添加到阵列中:

for (int i = 0; i < lotto.length; i++)
{
    int randomNumber = (Math.random() * 50);
    while(IsFound(randomNumber))
    {
        randomNumber = (Math.random() * 50);
    }
    lotto[i] = randomNumber;
}