【问题标题】:How to get a random number in one statement from a list of values;如何在一个语句中从值列表中获取随机数;
【发布时间】:2020-10-22 04:48:11
【问题描述】:

鉴于下面的代码,我必须找到一种从以下值中获取随机值的方法:100、120、140、160、180、200、220、240、260、280。关键是我必须编写一条语句(一个分号),它将随机选择一个整数并将其分配给 random_int 变量。有谁知道我如何创建上述数字的列表或数组并从数字中选择一个随机 int 以在单个语句中分配给 random_int ?感谢您的帮助!

  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);
    }

【问题讨论】:

    标签: java random


    【解决方案1】:

    你可以这样做:

    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())));
        }
    

    【讨论】:

      【解决方案2】:

      您可以使用Arrays 类从您的数组中创建一个集合

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

      【讨论】:

        【解决方案3】:

        将它们全部推入一个数组并随机排列数组的索引。一切都可以在一个语句中完成:

        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);
        }
        

        【讨论】:

        • 你试过编译吗?
        • 我做了,有什么问题吗?
        • 什么 JDK,不能为我编译。修改后就好了。
        • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
        • 已编辑。谢谢大家的建议!
        【解决方案4】:
        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);
            }
        
        }
        

        【讨论】:

          【解决方案5】:
          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。

          可能是一道考试题。

          【讨论】:

            【解决方案6】:

            下面给出的可以是完成这项工作的单一语句:

            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() 返回此列表中的元素数。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-08-19
              • 2023-02-25
              • 2017-12-29
              • 1970-01-01
              相关资源
              最近更新 更多