【问题标题】:Generating a random number in a loop在循环中生成随机数
【发布时间】:2017-02-25 10:28:46
【问题描述】:

我想知道如何在循环中生成随机数。我知道我应该使用java.util.random,但我想知道我应该怎么做,因为我需要生成程序需要的尽可能多的项目随机数,因为它的排序算法。

例如,首先,您需要选择您使用的方法(例如1),然后输入数字,然后您需要输入计数项目(例如5),然后,您需要输入项目(因此,如果您有 5 个项目,则需要输入 5 个不同的项目,然后进行排序。

我需要生成这些数字,因为如果我需要例如 10000 个计数,我不想手动输入 10k 个数字,因此最好自动输入。

希望你能帮助我!对不起,如果我错了或任何事情,这是我在这里的第一篇文章。 :)

附言我在此处添加了部分代码,以便您可以看到我的循环。

public static void main(String[] args) { //3.part - array output

    int numurs;

        int metode;
        System.out.println("Name Surname RDBF0 student no");
        System.out.print("method: ");

        Scanner sc = new Scanner(System.in);
        if (sc.hasNextInt()){
            numurs = sc.nextInt();
        } 
        else
        {
            System.out.println("input-output error");
            sc.close();
            return;
        }

        if (numurs != 1 && numurs != 2) {
            System.out.println("input-output error");
                sc.close();
                return;
        }



        System.out.print("count: ");
            if (sc.hasNextInt())
                metode = sc.nextInt();

            else {
                System.out.println("input-output error");
                sc.close();
                return;
            }


        int a[] = new int[metode];

        System.out.println("items: ");

        for (int i = 0; i < a.length; i++) {

            if (sc.hasNextInt())
            a[i] = sc.nextInt();

        else {
            System.out.println("input-output error");
            sc.close();
            return;
            }
        }
        sc.close();      

        switch (numurs) {

        case 1: firstMethod(a);
        break;
        case 2: secondMethod(a);
        break;

        default:
            System.out.println("input-output error");
        return;
        }
        System.out.println("sorted: ");

            for (int i = 0; i < a.length; i++)
                System.out.print(a[i] + " ");
        } 
    }

【问题讨论】:

  • 一个...循环怎么样?
  • @Jyr 我不想替换用户需要输入这些项目编号的行,因此util.random 可以自动填写所有需要的数字。这是发生计数的循环:System.out.print("count: "); if (sc.hasNextInt()) metode = sc.nextInt(); else { System.out.println("input-output error"); sc.close(); return; } int a[] = new int[metode];

标签: java loops random


【解决方案1】:

您可以按照您所说的使用 Random 类生成随机数:

Random rand = new Random();
//if you want the random number to be between 0 and maxValue
int randomNumber = rand.nextInt(maxValue);

//if you want it to be between minValue and maxValue, it should look like this
int randomNumber = rand.nextInt(maxValue) + minValue;

【讨论】:

  • 我应该在哪里写这些行?因为我有一个循环发生这个随机数输出。你可以看看确切的循环here
  • 当你想捕获你需要的值时,你可以使用上面的方法,而不是使用 Scanner。例如metode = rand.nextInt(5000);
  • 好吧,试了一下。没有编译错误或任何东西,但我看不出它为什么不起作用。所以here 就是我所做的。我是不是哪里错了?
  • 我尝试在任何地方使用此代码,但仍然没有任何区别
  • 尝试根据您的要求进行调整:Random rand = new Random(); int a[] = new int[metode]; System.out.println("项目:"); for (int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
相关资源
最近更新 更多