2014-07-03 81 views
0

我是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(); 
    } 
    } 

回答

0

首先,你在main中嵌套一個函數。我認爲你不能這樣做。請將它們分開(我會舉例說明),看看是否有幫助。另外,請注意用戶輸入。假設用戶將會犯錯,並且在防守方面進行編程。如果我要在這個程序中輸入「Hello」,該怎麼辦?它會拋出一個強硬的例外和崩潰。儘量防止這種事情。

此外,我不知道數組是否可以是passed by reference或不在Java中,所以我修改了排序方法以返回新的「排序」數組。

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(); 

    } 
+0

編輯我的錯誤與Vstring不在數量的函數調用。向任何人投下我的答案,請參閱:http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-downvotes –

+0

謝謝你回答這樣一個很好的幫助:) – JulzBlues

0

試試這個代碼,你應該叫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(); 
} 
+0

我會說標準做法doesn不涉及從插入排序方法中打印。否則看起來很好。 –

+0

@RickyMutschlechner,使用靜態int []列表屬性並使用它進行打印很好嗎? – user3717646

+0

我只想說從main打印會更好。就可重用性而言,如果某人不想每次打印都打印出來會怎樣?把它作爲一個選項而不是強制性的將是有益的。 –

-1

您不調用printValues函數。您也不要調用排序功能。一個函數內的代碼只有在您調用它時纔會執行。 此外,我會建議縮進代碼,以便兩個大括號內的東西縮進兩次等

+0

這段代碼甚至沒有編譯,很有可能更別說運行正常了。 –

0

問題是,你不是在main調用你的函數。你的代碼將被編譯,(儘管你可能想要一些縮進/空格來提高可讀性...... 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(); 
    } 
} 

我希望它幫助! :)