Java 如何打印随机插入排序的数组内容以及随机数之前的单词?

Java 如何打印随机插入排序的数组内容以及随机数之前的单词?,java,arrays,sorting,insertion-sort,Java,Arrays,Sorting,Insertion Sort,这段代码对我想要的随机数进行排序和打印。我缺少的是数字前面的单词。我不熟悉java,但更精通Python。我希望打印出来的每个随机数都表示以下内容: 结果[0]=_ 结果[1]=_ 结果[2]=_ 结果[3]=_ 而不必把每一个都一一放到代码中。 在Python中,代码如下所示,它将打印出所有内容 print(“result[”,I,“]=”,result[I]) publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 系统输出打印(“输入

这段代码对我想要的随机数进行排序和打印。我缺少的是数字前面的单词。我不熟悉java,但更精通Python。我希望打印出来的每个随机数都表示以下内容:

结果[0]=_

结果[1]=_

结果[2]=_

结果[3]=_

而不必把每一个都一一放到代码中。 在Python中,代码如下所示,它将打印出所有内容

print(“result[”,I,“]=”,result[I])

publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入随机数的大小”);
int n=input.nextInt();
随机=新随机();
int[]result=random.int(n,-n,n).toArray();
Arrays.stream(result).forEach(System.out::println);
System.out.println(“\n”);
int len=result.length;
System.out.println(“插入排序”);
对于(inti=1;i=0&&result[j]>key);j--{
结果[j+1]=结果[j];
结果[j+1]=键;
对于(i=0;i
这就是你要找的吗

for(i = 0; i < result.len; i++){
    System.out.println("result[" + i + "] = " + result[i]);
}  
for(i=0;i
这就是你要找的吗

for(i = 0; i < result.len; i++){
    System.out.println("result[" + i + "] = " + result[i]);
}  
for(i=0;i
您只需要替换这一行:
System.out.println(“result[”+(i)+“]=”;

对于此行:
System.out.println(“结果[”+(i)+“]=”+结果[i]);

或者您可以使用
字符串。格式

System.out.println(String.format(“结果[%d]=%s”,i,结果[i]);

您还必须删除其他两行:

// here you are printing again all the numbers so is not necessary
Arrays.stream(result).forEach(System.out::println); 

// here you are adding another linebreak, but using println add the linebreak automatically
System.out.println("\n");

您只需要替换这一行:
System.out.println(“result[”+(i)+“]=”;

对于此行:
System.out.println(“结果[”+(i)+“]=”+结果[i]);

或者您可以使用
字符串。格式

System.out.println(String.format(“结果[%d]=%s”,i,结果[i]);

您还必须删除其他两行:

// here you are printing again all the numbers so is not necessary
Arrays.stream(result).forEach(System.out::println); 

// here you are adding another linebreak, but using println add the linebreak automatically
System.out.println("\n");

问题是您将print语句放在嵌套循环中。此外,在嵌套循环的每次迭代中都打印整个数组。只需将其取出,并在一次迭代中只打印一个元素

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

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter Size of Random Number: ");
        int n = input.nextInt();
        Random random = new Random();
        int[] result = random.ints(n, -n, n).toArray();

        Arrays.stream(result).forEach(System.out::println);
        System.out.println("\n");

        // Insertion sort
        int len = result.length;
        for (int i = 1; i < len; ++i) {
            int key = result[i];
            int j = i - 1;
            while (j >= 0 && result[j] > key) {
                result[j + 1] = result[j];
                j = j - 1;
            }
            result[j + 1] = key;
        }

        System.out.println("After sorting: ");
        for (int i = 0; i < len; i++) {
            System.out.println("result[" + (i) + "] =" + result[i]);
        }
    }
}

注意:我还更正了您的排序逻辑。internet上有数百个教程可以学习插入排序,例如,您可以检查。

问题是您已将print语句放入嵌套循环中。此外,您在嵌套循环的每次迭代中都打印整个数组。就这样吧在一次迭代中只输出和打印一个元素

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

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter Size of Random Number: ");
        int n = input.nextInt();
        Random random = new Random();
        int[] result = random.ints(n, -n, n).toArray();

        Arrays.stream(result).forEach(System.out::println);
        System.out.println("\n");

        // Insertion sort
        int len = result.length;
        for (int i = 1; i < len; ++i) {
            int key = result[i];
            int j = i - 1;
            while (j >= 0 && result[j] > key) {
                result[j + 1] = result[j];
                j = j - 1;
            }
            result[j + 1] = key;
        }

        System.out.println("After sorting: ");
        for (int i = 0; i < len; i++) {
            System.out.println("result[" + (i) + "] =" + result[i]);
        }
    }
}

注意:我还更正了您的排序逻辑。互联网上有数百个教程可以学习插入排序,例如,您可以检查。

有没有办法更改它,以便它可以使用插入进行排序。这不必进行任何排序。我是否需要将system.out.println放入for的内部执行排序的循环?是的,如果要打印每次迭代的结果,必须将最内层的for循环替换为该for循环。是否有方法可以更改它,以便它可以使用插入进行排序。这不必执行任何排序。是否需要将system.out.println放入执行排序的for循环中?是的,如果要打印每次迭代的结果,必须将最内层的for循环替换为该for循环。谢谢。如果我要将其从最大值更改为最小值,我需要更改什么?非常欢迎。对于降序,只需将条件更改为
result[j]
保持其他所有内容不变。如有任何其他疑问,请随时发表评论。谢谢。如果我要将其从最大更改为最小,我需要更改什么?非常欢迎。对于降序,只需将条件更改为
结果[j]
保持其他一切不变。如有任何疑问,请随时发表评论。