在Java中仅返回奇数(int)的方法

时间:2018-08-01 08:39:14

标签: java random do-while

因此,我正在尝试创建一种方法,该方法返回范围(1-43)内的随机数,但仅返回奇数。我使用了do-while语句,但是该程序不断返回许多数字-偶数和奇数都一样。您能否告诉我它有什么问题,所以它只会返回所请求的数字类型(取决于:%= 0/1)?

public static String generateRandomOddNumbersWithinRange(){

    int randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);

    do {
        return Integer.toString(randomNum);

    } while (randomNum % 2 == 1);
}

6 个答案:

答案 0 :(得分:8)

您无条件返回。相反,您应该循环播放直到数字符合条件,然后才返回:

public static String generateRandomOddNumbersWithinRange(){

    int randomNum = 0;
    do {
        randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);
    } while (randomNum % 2 == 0);
    return Integer.toString(randomNum);
}

或者,更可靠的解决方案是将0到21之间的数字随机化,然后将其加倍并加一个:

public static String generateRandomOddNumbersWithinRange(){
    return Integer.toString(ThreadLocalRandom.current().nextInt(0, 21 + 1) * 2 + 1);
}

答案 1 :(得分:1)

您需要不断生成随机数,直到得到一个奇数为止,因此int randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1)应该在循环内。另外,请勿立即返回!首先,检查数字是否确实为奇数,并仅在为真时才返回。这样,您甚至可以从循环外部返回。像这样:

int randomNum = 0;
do {
  randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);
} while (randomNum % 2 == 1);

return Integer.toString(randomNum);

答案 2 :(得分:0)

在返回奇数或偶数之前返回值

public static String generateRandomOddNumbersWithinRange(){    
    int randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);

    do {
        return Integer.toString(randomNum); // return the value

    } while (randomNum % 2 == 1); // check for odd or even
}

在检查后放入return语句。像这样:

public static String generateRandomOddNumbersWithinRange(){
    int randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);
    while ( randomNum % 2 == 0 ) {
      // if not odd, overwrite value
      randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);
    }
    return Integer.toString(randomNum);
}

答案 3 :(得分:0)

您的代码只运行一次就可以立即返回一个值,无论它是什么。而是这样做:

public static String generateRandomOddNumbersWithinRange(){

    int randomNum;

    do {
        randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);

    } while (randomNum % 2 == 0);

    return Integer.toString(randomNum);
}

当randomNum / 2剩下任何东西时,这会一直为randomNum分配一个随机数。

答案 4 :(得分:0)

您不需要使用do-while。这是您需要使用的方法:

public static int generateRandomOddNumbersWithinRange(final int min, final int max) {
    Random random = new Random();
    int randomNumber = random.nextInt((max - min) + 1) + min;

    if ((randomNumber & 1) != 0) {
        return randomNumber;
    }

    return randomNumber - 1;
}

答案 5 :(得分:0)

难道您不能将它加倍再加1吗?

public static int generateRandomOddNumber(int max) {
    int randomNum = ThreadLocalRandom.current().nextInt(0, ((max - 1) / 2) + 1);
    return (randomNum * 2) + 1;
}

public void test() {
    Map<Integer, Integer> counts = new HashMap<>();
    for (int i = 0; i < 10000; i++) {
        int r = generateRandomOddNumber(43);
        // How many of that value have we had so far?
        int count = counts.containsKey(r) ? counts.get(r) : 0;
        // We've just had another one.
        counts.put(r, count + 1);
    }
    // Pull the results out into a list.
    List<Map.Entry<Integer, Integer>> countsList = new ArrayList<>(counts.entrySet());
    // Sort them.
    Collections.sort(countsList, Comparator.comparingInt(Map.Entry::getKey));
    System.out.println(countsList);
}

此打印:

  

[1 = 460,3 = 463,5 = 445,7 = 441,9 = 407,11 = 467,13 = 438,15 = 483,17 = 454,19 = 463,21 = 501,23 = 444、25 = 455、27 = 419、29 = 425、31 = 460、33 = 492、35 = 449、37 = 440、39 = 495、41 = 463、43 = 436]

看起来好像做对了。