我的问题是当布尔值为false时它仍然返回i并将其放在数组乐透中。 我如何修复它,以便当布尔值为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"); } } }My issue is the when boolean is false it still returns i and places it in the array lotto. How do I fix it so that when boolean is false it will drop i and run a another random number for that element.
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"); } } }最满意答案
我不完全确定你要做什么 - 你实际上并没有做任何事情与isFound返回的布尔值。 但是,如果你问你是否只能分配lotto [i]如果isFound评估为true,那么你可以这样做的一种方法是更改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); }此代码的执行时间与您需要获取的元素数量成正比,而不是在完全随机的时间内,并且您不需要重复循环。
I'm not entirely sure what you're trying to do - you don't actually do anything at all with the boolean value returned by isFound. But, if you're asking whether you can only assign lotto[i] if isFound evaluates to true, one way you could do that is to change the for loop as follows:
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--; } }Your design is not terrifically efficient or well-designed - you're counting on a random number you generate not having been seen before, which gets significantly worse performance-wise the more numbers you've already created. A better way you could do this would be to just take a single element of a pre-existing collection of non-repeating integers in the range you want:
// 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); }This code executes in an amount of time directly proportional to the number of elements you need to get, rather than in a totally random amount of time, and you don't need to fudge around with repeating for loops.
更多推荐
发布评论