如何在一条语句中从值列表中获取随机数;

huangapple go评论110阅读模式
英文:

How to get a random number in one statement from a list of values;

问题

给定下面的代码,我必须找到一种方法从值中获取一个随机值:100、120、140、160、180、200、220、240、260、280。问题在于,我必须编写一条单独的语句(一个分号),这条语句将随机选择其中一个整数,并将其赋值给random_int变量。是否有人有任何想法,我如何能够创建一个包含上述数字的列表或数组,并从中随机选择一个整数以赋值给random_int,只用一条语句?谢谢帮助!

public static void main(String[] args) {
    Random random = new Random();
    int random_int;
    // 单条语句放在这里
    System.out.println("Number is: " + random_int);
}
英文:

Given the code below I have to find a way to get a random value from the values: 100, 120, 140, 160, 180, 200, 220, 240, 260, 280. The catch is that I have to write a single statement (One semi-colon) that will randomly pick one of the ints and assign it into the random_int variable. Does anyone have any idea how I could create a list or array of the numbers above and pick a random int from the numbers to assign to random_int in a single statement? Thank you for the help!

  public static void main(String[] args) {
    Random random = new Random();
     int random_int;
    // Your single statement goes here
    System.out.println(“Number is: “ + random_int);
    }

答案1

得分: 0

将它们全部推入一个数组中,并随机数组的索引。所有操作都可以在一个单独的语句中完成:

public static void main(String[] args) {
    Random random = new Random();
    int random_int = new int[]{100, 120, 140, 160, 180, 200, 220, 240, 260, 280}[random.nextInt(10)];
    System.out.println("数字为:" + random_int);
}
英文:

Push them all into an array and random the array's index. All can be done in one single statement:

public static void main(String[] args) {
    Random random = new Random();
    int random_int = new int[]{100, 120, 140, 160, 180, 200, 220, 240, 260, 280}[random.nextInt(10)];
    System.out.println("Number is: " + random_int);
}

答案2

得分: 0

你可以利用 Arrays 类从你的数组创建一个集合

System.out.println(Arrays.asList(100, 120, 140, 160, 180, 200, 220, 240, 260, 280).
                                                          get(new Random().nextInt(10)));
英文:

You can make use of the Arrays class to create a collection from your array

System.out.println(Arrays.asList(100, 120, 140, 160, 180, 200, 220, 240, 260, 280).
                                                          get(new Random().nextInt(10)));

答案3

得分: 0

Sure, here's the translated code snippet:

你可以这样做

    public static void main(String[] args) {
        Random random = new Random();
        List<Integer> integerList = Arrays.asList(100, 120, 140, 160, 180, 200, 220, 240, 260, 280);
        System.out.println(integerList.get(random.nextInt(integerList.size())));
    }
英文:

You can do something like:

public static void main(String[] args) {
        Random random = new Random();
        List&lt;Integer&gt; integerList = Arrays.asList(100, 120, 140, 160, 180, 200, 220, 240, 260, 280);
        System.out.println(integerList.get(random.nextInt(integerList.size())));
    }

答案4

得分: 0

int random_int = new Random().nextInt(10) * 20 + 100;
int random_int = (new Random().nextInt(10) + 5) * 20;

nextInt(10)将会给出0到9的数字。(9 =(280 - 100)/ 20)。

关键在于看到所期望的数字中的规律:以20为步长,从100开始到280。

可能是一个考试题目。

英文:
int random_int = new Random().nextInt(10)*20 + 100;
int random_int = (new Random().nextInt(10) + 5)*20;

nextInt(10) will give 0..9. (9 = (280 - 100)/20).

It is a matter of seeing the regularities in the desired numbers: steps of 20, starting with 100 upto 280.

Could have been an exam question.

答案5

得分: 0

public class Foo {

    public static void main(String[] args) {
        for (int i = 0; i < NUMBERS.size(); i++)
            System.out.println(getRandomNumber());
    }

    private static final List<Integer> NUMBERS = Arrays.asList(100, 120, 140, 160, 180, 200, 220, 240, 260, 280);

    public static int getRandomNumber() {
        Collections.shuffle(NUMBERS);
        return NUMBERS.get(0);
    }

}
英文:
public class Foo {

    public static void main(String[] args) {
        for (int i = 0; i &lt; NUMBERS.size(); i++)
            System.out.println(getRandomNumber());
    }

    private static final List&lt;Integer&gt; NUMBERS = Arrays.asList(100, 120, 140, 160, 180, 200, 220, 240, 260, 280);

    public static int getRandomNumber() {
        Collections.shuffle(NUMBERS);
        return NUMBERS.get(0);
    }

}

答案6

得分: 0

以下是已翻译的内容:

给出下面可以执行任务的单个语句:

random_int = List.of(100, 120, 140, 160, 180, 200, 220, 240, 260, 280)
                .get(random.nextInt(List.of(100, 120, 140, 160, 180, 200, 220, 240, 260, 280).size()));

解释:

  1. Random#nextInt(int bound) 返回一个介于 0(包括)和 bound(不包括)之间的 int 值。
  2. List#get(int index) 返回列表中指定位置的元素。
  3. List#size() 返回列表中的元素数量。
英文:

Given below can be the single statement to do the job:

random_int = List.of(100, 120, 140, 160, 180, 200, 220, 240, 260, 280)
				.get(random.nextInt(List.of(100, 120, 140, 160, 180, 200, 220, 240, 260, 280).size()));

Explanation:

  1. Random#nextInt(int bound) returns an int value between 0 (inclusive) and bound (exclusive).
  2. List#get(int index) returns the element at the specified position in this list.
  3. List#size() returns the number of elements in this list.

huangapple
  • 本文由 发表于 2020年10月22日 12:48:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64475490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定