生成一系列随机值以进行打印。排序并再次打印。Java

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

Generating a sequence of random values to print. Sorting and printing again. Java

问题

import java.util.Arrays;

public class Array {

	public static void main(String[] args) {
		// 创建一个包含20个整数的数组。
		int[] array = new int[20];
		for (int i = 0; i < array.length; i++)
		{
			array[i] = (int) (Math.random() * 99 + 1);
		}
		// 打印随机数列。
		System.out.print("随机数列: ");

	    for (Integer i : array) {
	        System.out.print(i.intValue() + " ");
	    }
	    System.out.println("");
	    System.out.println("");
	    // 打印排序后的数列。
	    System.out.print("排序后的数列: ");
		Arrays.sort(array);
		System.out.println(Arrays.toString(array));

	}

}
英文:

Hello I'm still a beginner in Java and I needed to write a program that generates a sequence of 20 random values between 0 and 99 in an array, prints the sequence, sorts it, and then prints the sorted sequence. I have the code below it works perfectly for my assignment. It prints the set of random values and then prints the sorted set of values. Do you know if there is any way to simplify the code that I have like in how to print the random values for example? I think there may be a way to simplify the code in its entirety but I'm not too sure. Please let me know if you have some suggestions to make to code look cleaner or easier.

import java.util.Arrays;

public class Array {

	public static void main(String[] args) {
		//This will create an array to hold 20 integers.
		int[] array = new int[20];
		for (int i = 0; i &lt; array.length; i++)
		{
			array[i] = (int) (Math.random() * 99 + 1);
		}
		//This will print the random sequence of values.
		System.out.print(&quot;Random sequence of values: &quot;);

	    for (Integer i : array) {
	        System.out.print(i.intValue() + &quot; &quot;);
	    }
	    System.out.println(&quot;&quot;);
	    System.out.println(&quot;&quot;);
	    //This will print the sorted sequence of values.
	    System.out.print(&quot;Sorted sequence of values: &quot;);
		Arrays.sort(array);
		System.out.println(Arrays.toString(array));

	}

}

答案1

得分: 2

好的,以下是翻译好的代码部分:

import java.util.Arrays;
import java.util.Random;

public class Array {

    public static void main(String[] args) {
        Random random = new Random();
        // 这将创建一个包含20个整数的数组。
        int[] array = new int[20];
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100);
        }
        // 这将打印随机值的序列。
        System.out.println("随机值序列: " + Arrays.toString(array));

        Arrays.sort(array);
        // 这将打印排序后的值的序列。
        System.out.println("排序后的值序列: " + Arrays.toString(array));
    }
}

请注意,我只翻译了代码部分,不包含任何额外内容。

英文:

Well, something this:

new Random().ints(20,0,100).forEach(System.out::println);

or if you want to print sorted

new Random().ints(20,0,100).sorted().forEach(System.out::println);

Or, if you want to avoid streams

import java.util.Arrays;
import java.util.Random;

public class Array {

    public static void main(String[] args) {
        Random random = new Random();
        //This will create an array to hold 20 integers.
        int[] array = new int[20];
        for (int i = 0; i &lt; array.length; i++)
        {
            array[i] = random.nextInt(100);
        }
        //This will print the random sequence of values.
        System.out.println(&quot;Random sequence of values: &quot; + Arrays.toString(array));

        Arrays.sort(array);
        //This will print the sorted sequence of values.
        System.out.println(&quot;Sorted sequence of values: &quot; + Arrays.toString(array));
    }
}

答案2

得分: 1

回答您的具体问题,您可以使用以下代码生成随机数。

Random r = new Random();

然后对于每次调用随机数,使用以下代码:

// 生成介于0和99之间的随机数(包括0和99)。
array[i] = r.nextInt(100);

要打印这些值,您可以使用以下代码:

for (int i : array) {
   System.out.print(i + " ");
}

流方法也是可行的,但在您熟悉Java基础之前,建议继续使用您已经有的方法。

英文:

To answer your specific question you can use this to generate the random numbers.

Random r = new Random();

Then for each call to random use:

// generates between 0 and 99 inclusive.
array[i] = r.nextInt(100);

To print the values you can do this:

for (int i : array) {
   System.out.print(i + &quot; &quot;);
}

The streams approach is also possible but I would stick with what you have until you get familiar with the basics of Java.

huangapple
  • 本文由 发表于 2020年4月4日 00:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61016768.html
匿名

发表评论

匿名网友

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

确定