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

How do I print the array contents for a random insertion sort along with the words before the random number?

此代码排序并打印我想要的随机数。我缺少的是数字之前的单词。我是 java 的新手,但更精通 Python。我希望每个随机数在打印出来时说出以下内容:

结果[0] = _

结果[1] = _

结果[2] = _

结果[3] = _

无需将这些一一放入代码中。 在 Python 中,代码如下,它将全部打印出来。

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

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");
        
       
        int len = result.length;
        System.out.println("Insertion Sort");
        for(int i=1; i<len; i++){
            int j;
            int key = result[i];
            for (j=i-1; (j >= 0 && result[j] > key); j--) { 
                result[j + 1] = result[j]; 
                result[j+1] = key;
                for( i = 0; i < len; i++){
                    System.out.println("result[" + (i) + "] =" );
                    Arrays.stream(result).forEach(System.out::println);
                    System.out.println("\n");
                }  
            } 
            
        }
    }

这是您要找的吗?

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

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

对于这一行:System.out.println("result[" + (i) + "] =" + result[i]);

或者您可以使用 String.format:

System.out.println(String.format("result[%d] = %s", i, result[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]);
        }
    }
}

样本运行:

Enter Size of Random Number: 5
0
-5
-3
2
1


After sorting: 
result[0] =-5
result[1] =-3
result[2] =0
result[3] =1
result[4] =2

注意:我也更正了您的排序逻辑。互联网上有数百个教程可用于学习插入排序,例如你可以查看 this one.