【问题标题】:Insertion Sort. Cannot print sorted values插入排序。无法打印排序值
【发布时间】:2014-07-03 16:19:40
【问题描述】:

我是 java 新手,我们的任务是进行插入排序,但我的代码有一点问题我无法打印我的排序值我不知道我的错误在哪里......请帮助我.谢谢你

import java.util.Scanner;
public class Sorting {

public static void main(String[] args){
System.out.println("Enter 10 numbers to be sorted separated with spaces:");
String values = new Scanner(System.in).nextLine();
 String [] Vstring = values.split(" ");
 int [] num = new int[Vstring.length];

 for(int i = 0; i<num.length; i++){
    num[i]=Integer.parseInt(Vstring[i]);

     }

     }
     public static void insertionSort(int[] list, int n){
     for(int i = 0; i<n; i++)
      {
        int key = list[i];
        int j = i-1;
        while(j >=0 && list[j]>key){
            list[j+1] = list[j];
            j=j-1;
        }

        list[j+1] = key;
        }


        }

       public static void printValues(int [] list){
       for (int i = 0; i<list.length; i++){
           System.out.println(list[i]+"");
       }
      System.out.println();
     }
     }

【问题讨论】:

    标签: java for-loop insertion-sort


    【解决方案1】:

    首先,您在 main 中嵌套了一个函数。我不认为你能做到这一点。请将它们分开(我将展示一个示例),看看是否对您有帮助。此外,请注意用户输入。假设用户会犯错误,并进行防御性编程。如果我在这个程序中输入“Hello”会怎样?它会抛出一个硬异常并崩溃。尽量防止这种事情发生。

    另外,我不知道数组在 Java 中是否可以是 passed by reference,所以我修改了你的排序方法以返回新的“排序”数组。

    public static void main(String[] args){
    
    System.out.println("Enter 10 numbers to be sorted separated with spaces:");
    
    String values = new Scanner(System.in).nextLine();
    
     String [] Vstring = values.split(" ");
     int [] num = new int[Vstring.length];
    
     for(int i = 0; i< num.length; i++){
          num[i] = Integer.parseInt(Vstring[i]);
     }
    
     num = insertionSort(num, num.length);
     printValues(num);
    
    }
    
     public static int[] insertionSort(int[] list, int n){
         for(int i = 0; i<n; i++)
          {
            int key = list[i];
            int j = i-1;
            while(j >=0 && list[j]>key){
                list[j+1] = list[j];
                j=j-1;
            }
    
            list[j+1] = key;
            }
    
          return list;
    
      }
    
    
      public static void printValues(int [] list){
           for (int i = 0; i<list.length; i++){
               System.out.println(list[i]+"");
           }
    
           System.out.println();
    
      }
    

    【讨论】:

    【解决方案2】:

    问题是你没有在 main.js 中调用你的函数。您的代码将编译,(尽管您可能希望有一些缩进/空格以提高可读性... Ctrl+A 然后 Ctrl+I 会有所帮助)但您不会得到任何结果。

    这是你必须做的:

    import java.util.Scanner;
    public class Sorting {
    
        public static void main(String[] args){
    
            System.out.println("Enter 10 numbers to be sorted separated with spaces:");
            String values = new Scanner(System.in).nextLine();
            String [] Vstring = values.split(" ");
            int [] num = new int[Vstring.length];
    
            for(int i = 0; i<num.length; i++){
                num[i]=Integer.parseInt(Vstring[i]);
            }
    
            //HERE!!!!! Call the functions!
            insertionSort(num, num.length); 
            printValues(num);
    
        }
    
        public static void insertionSort(int[] list, int n){
            for(int i = 0; i<n; i++)
            {
                int key = list[i];
                int j = i-1;
                while(j >=0 && list[j]>key){
                    list[j+1] = list[j];
                    j=j-1;
                }
    
                list[j+1] = key;
            }
        }
    
        public static void printValues(int [] list){
    
            for (int i = 0; i<list.length; i++){
                System.out.println(list[i]+"");
            }
            System.out.println();
        }
    }
    

    希望对您有所帮助! :)

    【讨论】:

      【解决方案3】:

      试试这段代码,你应该在main方法中调用insertionSort(num, num.length)方法和printValues(int[] list)。我也修改了insertionSort方法的返回类型。

      public static void main(String[] args) {
          System.out.println("Enter 10 numbers to be sorted separated with spaces:");
          String values = new Scanner(System.in).nextLine();
          String[] Vstring = values.split(" ");
          int[] num = new int[Vstring.length];
      
          for (int i = 0; i < num.length; i++) {
              num[i] = Integer.parseInt(Vstring[i]);
      
          }
      
          int[] result=insertionSort(num, num.length);
          printValues(result);
      }
      
      public static int[] insertionSort(int[] list, int n) {
          for (int i = 0; i < n; i++) {
              int key = list[i];
              int j = i - 1;
              while (j >= 0 && list[j] > key) {
                  list[j + 1] = list[j];
                  j = j - 1;
              }
      
              list[j + 1] = key;
          }
          return list;
      }
      
      public static void printValues(int[] list) {
          for (int i = 0; i < list.length; i++) {
              System.out.println(list[i] + "");
          }
          System.out.println();
      }
      

      【讨论】:

      • 我想说标准做法不涉及从 insertSort 方法中打印。否则看起来很好。
      • @RickyMutschlechner ,使用静态 int [] 列表属性并使用它进行打印好吗?
      • 我只想说从 main 打印会更好。在可重用性方面,如果有人不想在每次排序时都打印怎么办?将其作为一个选项而不是强制性的会是有益的。
      • @RickyMutschlechner,好的,我明白了。提前致谢!
      • 我做了编辑,谢谢你的好意@RickyMutschlechner
      【解决方案4】:

      您不调用 printValues 函数。您也不要调用排序函数。函数内的代码只有在你调用它时才会执行。 另外我建议缩进代码,以便两个大括号内的内容缩进两次等。

      【讨论】:

      • 这段代码很可能无法编译,更不用说正常运行了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多