2016-10-28 31 views
-1

私はこの3つの単語のアルファベットを表すコードを持っています。私は一番下のメソッドを削除しようとしているだけで、上のコードにメソッドを追加するだけです。しかし、私はそうするための作業方法を理解することはできません。どんな指導や助けもありがとう、ありがとう!2つの方法をマージする

System.out.print("Please enter three words: "); 
    final String words = keyboard.nextLine(); 
    final String[] parts = words.split(" "); 

    if (parts[0].equals(parts[1]) && parts[1].equals(parts[2])) { 
    System.out.println("All three of those words are the same!"); 

} else { 

     System.out.print("In alphabetical order those are: "); 
     alphabetizing (parts); 
     final int three = 3; 
     for (int limit = 0; limit < three; limit++) { 
     System.out.print(parts[limit] + " "); 
     } 
    } 
    } 

    public static void alphabetizing (final String[] parts) { 
     int word; 
     boolean check = true; 
     String temp; 

       for (word = 0; word < parts.length - 1; word++) { 
         if (parts[ word ].compareToIgnoreCase(parts[word + 1]) > 0) { 
            temp = parts[word]; 
            parts[word] = parts[word + 1]; 
            parts[word + 1] = temp; 
            check = true; 

         } 
       } 
     } 
    } 
+0

なぜあなたはこれをしたいのでしょうか? –

+1

コードを他の方法で抽出する必要があります。 – Tom

+0

@ScaryWombatこれは割り当てのためのものです。私は方法を削除するように要求された。 – Derrin

答えて

0

とにかくやりたいからです。なぜ私はそれが動作しません問題を参照してくださいしないでください。メソッドを呼び出すelseステートメントにそれを追加するだけで動作します。

System.out.print("Please enter three words: "); 
    Scanner keyboard = new Scanner(System.in); 
    final String words = keyboard.nextLine(); 
    final String[] parts = words.split(" "); 

    if (parts[0].equals(parts[1]) && parts[1].equals(parts[2])) { 
     System.out.println("All three of those words are the same!"); 

    } else { 

     System.out.print("In alphabetical order those are: "); 

     //boolean check = true; you don't neeed this. 
     String temp = ""; 

     for (int word = 0; word < parts.length - 1; word++) { 
      if (parts[word].compareToIgnoreCase(parts[word + 1]) > 0) { 
       temp = parts[word]; 
       parts[word] = parts[word + 1]; 
       parts[word + 1] = temp; 

      } 
     } 
     final int three = 3; 
     for (int limit = 0; limit < three; limit++) { 
      System.out.print(parts[limit] + " "); 
     } 

    } 

出力:

Please enter three words: a b c 
In alphabetical order those are: a b c 
Please enter three words: a a a 
All three of those words are the same! 
関連する問題