2017-06-11 11 views
-1

最大100個の乱数を生成して表示するプロジェクトを行う必要があります。次に、いくつかの種類の選択(選択、バブル、挿入、シェル..)それらを並べ替える。私はすでに生成部分を行っていますが、最初のソート選択ソートに問題があります。生成され、ソートされた数字の代わりにゼロだけが表示されます。あなたのコードでより大きなプロジェクトでの選択ソートの実装

package projekt; 

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

public class Projekt { 

    public static void main(String[] args) { 
     int[] tablica; 
     Scanner odczyt = new Scanner(System.in); 

     System.out.println("Give number of elements (max 100): "); 
     int liczbaElementow = odczyt.nextInt(); 
     tablica = new int[liczbaElementow]; 

     if (liczbaElementow <= 100 && liczbaElementow > 0){ 
      System.out.println("Generated:"); 
      Random Generator = new Random(); 
      for (int idx = 1; idx <= liczbaElementow; ++idx){ 
       int randomInt = Generator.nextInt(101); 
       System.out.print(" " +randomInt); 
      } 

      { 
       System.out.println(" "); 
       System.out.print("Choose sorting method (1, 2 lub 3):"); 
      } 
      Scanner odczyt2 = new Scanner(System.in); 
      int ktoresort = odczyt2.nextInt(); 

      switch (ktoresort) { 
       case 1: System.out.println("SORT 1"); 
       { 
        { 
         int min = 0; 

         for(int i = 0;i<liczbaElementow;i++) 
         { 
          min = i; 
          for(int j = i + 1;j<liczbaElementow;j++) 
          { 
           if(tablica[j] < tablica[min]) { min = j;} 
          } 
          int temp = tablica[i]; 
          tablica[i] = tablica[min]; 
          tablica[min] = temp; 
          System.out.println(tablica[i]); 
         } 
        } 
       } 
       break; 
       case 2: System.out.println("SORT 2"); 
       break; 
       case 3: System.out.println("SORT 3"); 
       break; 
       default: System.out.println("incorrect value"); 
       break; 
      } 

      odczyt2.close();  
     } 
     else if (liczbaElementow==0) { 
      System.out.println("entered 0");   
     } 

     else{ 
      System.out.println("value is biger than 100"); 
     } 
    } 
} 
+0

ソリューションに私の友人を持って追加しませんか? – Ollaw

+0

スタックオーバーフローへようこそ!私は余分な行を削除し、インデントを修正するためにあなたの質問を編集しました。あなたの問題には関係ないが、あなたがプログラマではないと言われて以来、あなたはそれによってより明白になっている不必要な括弧を持っています。可能であれば、あなたのIDEを自動インデントに設定することをお勧めします。また、 'System.out.println(" ")'、 'System.out.println()'の引数が必要なくなり、新しい行だけが出力されます。乾杯! – whrrgarbl

+0

ありがとうございます。私は古典的な窓のメモ帳でこれを書いて始めました、そして、私の友人は日食について教えてくれました。 私は本当に新しい行を作成するためにちょうど()を入力しようとしたことはありませんでしたが、今私のコードでこれを置き換えました。それはうまくいった: – justme

答えて

0

、あなたはここでtablicaに生成された値を使用して、配列を埋めるん

public class Projekt { 

public static void main(String[] args) { 


    int[] tablica; 
    Scanner odczyt = new Scanner(System.in); 

    System.out.println("Give number of elements (max 100): "); 
    int liczbaElementow = odczyt.nextInt(); 
    tablica = new int[liczbaElementow]; 


    if (liczbaElementow <= 100 && liczbaElementow > 0){ 

     System.out.println("Generated:"); 
     Random Generator = new Random(); 
     for (int idx = 1; idx <= liczbaElementow; ++idx){ 
      int randomInt = Generator.nextInt(101); 
      System.out.print(" " +randomInt); 
      tablica[idx-1]=randomInt; 


     } 

     { 
      System.out.println(" "); 
      System.out.print("Choose sorting method (1, 2 lub 3):"); 
     } 
     Scanner odczyt2 = new Scanner(System.in); 
     int ktoresort = odczyt2.nextInt(); 

     switch (ktoresort) { 
      case 1: System.out.println("SORT 1"); 
       bubbleSort(tablica); 
      break; 
      case 2: System.out.println("SORT 2"); 
       insertionSort(tablica); 
      case 3: System.out.println("SORT 3"); 
       shelSort(tablica); 
       break; 
      default: System.out.println("incorrect value"); 
       break; 
     } 

     System.out.println(Arrays.toString(tablica)); 
     odczyt2.close(); 
    } 
    else if (liczbaElementow==0) { 
     System.out.println("entered 0"); 
    } 

    else{ 
     System.out.println("value is biger than 100"); 
    } 
} 


public static void bubbleSort(int [ ] num) 
{ 
    int j; 
    boolean flag = true; // set flag to true to begin first pass 
    int temp; //holding variable 

    while (flag) 
    { 
     flag= false; //set flag to false awaiting a possible swap 
     for(j=0; j < num.length -1; j++) 
     { 
      if (num[ j ] < num[j+1]) // change to > for ascending sort 
      { 
       temp = num[ j ];    //swap elements 
       num[ j ] = num[ j+1 ]; 
       num[ j+1 ] = temp; 
       flag = true;    //shows a swap occurred 
      } 
     } 
    } 
} 


public static void insertionSort(int [ ] num) 
{ 
    int j;      // the number of items sorted so far 
    int key;    // the item to be inserted 
    int i; 

    for (j = 1; j < num.length; j++) // Start with 1 (not 0) 
    { 
     key = num[ j ]; 
     for(i = j - 1; (i >= 0) && (num[ i ] < key); i--) // Smaller values are moving up 
     { 
      num[ i+1 ] = num[ i ]; 
     } 
     num[ i+1 ] = key; // Put the key in its proper location 
    } 
} 


public static void shelSort(int[] array) { 
    int inner, outer; 
    int temp; 

    int h = 1; 
    while (h <= array.length/3) { 
     h = h * 3 + 1; 
    } 
    while (h > 0) { 
     for (outer = h; outer < array.length; outer++) { 
      temp = array[outer]; 
      inner = outer; 

      while (inner > h - 1 && array[inner - h] >= temp) { 
       array[inner] = array[inner - h]; 
       inner -= h; 
      } 
      array[inner] = temp; 
     } 
     h = (h - 1)/3; 
    } 
} 

}

+0

ありがとう!なぜ私がtablicaに値を追加しなかったのかわかりませんが、これは今や意味があります。もう一度選択してソートを追加しようとします。もう一度問題が発生した場合は戻ってきます:) – justme

+0

この回答があなたの質問を解決した場合は、チェックマークをクリックして受け入れることを検討してください。これは、あなたが解決策を見つけ出し、回答者とあなた自身の両方に評判を与えていることを広範なコミュニティに示します。これを行う義務はありません – urag

+0

書き込みを完了する前に、これをしばらく開いたままにしておきたいと思います。選択ソートを追加して、すべてが機能していますので、今すぐ受け入れます – justme

関連する問題