2017-10-25 22 views
2

ここに私のプログラムがあり、何が間違っているのか分かりません。 Alphabetizingメソッドを削除しても問題ありません。誰かがこのメソッドの何が問題なのか、それを修正する方法を教えてください。最初の部分は、表示されるように言葉を印刷するはずです、私はそれらを大文字にすることになっている2番目の部分、次にハイフンを追加し、単語の間のスペースを削除するはずです最後の部分は、それらをアルファベット順に並べます。ここでJavaのアルファベット順の単語

import static java.lang.System.*; 

public class WordFun { 

    private String word, loc, changedWord; 

    public WordFun() { 
    } 

    public WordFun(String w) { 
     word = w; 
     changedWord = w; 
    } 

    public void setWord(String w) { 
    } 

    public void makeUpper() { 
     changedWord = word.toUpperCase(); 
    } 

    public void addHyphen() { 
     String hyphen = word.trim(); 
     changedWord = word.replaceAll(" ", "-"); 
    } 

    public void alphabetize() { 
     int loc; 
     loc = original.indexOf(" "); 
     String wordOne = original.substring(0, loc); 
     String wordTwo = original.substring(loc + 1, word.length()); 
     if (wordOne.compareTo(wordTwo) > 0) { 
      word = wordTwo + " " + wordOne; 
     } else { 
      word = wordOne + " " + wordTwo; 
     } 
    } 

    public String toString() { 
     return changedWord; 
    } 
} 

ランナープログラムは、属性originalがあなたのalphabetize()方法

に欠けている

import static java.lang.System.*; 

public class WordFunRunner { 

    public static void main(String args[]) { 
     WordFun test = new WordFun("Hello World"); 
     out.println(test); 
     test.makeUpper(); 
     out.println(test); 
     test.addHyphen(); 
     out.println(test); 
     test.alphabetize(); 
     out.println(test); 

//add more test cases 
     WordFun test1 = new WordFun("Jeroo Bob"); 
     out.println(test1); 
     test1.makeUpper(); 
     out.println(test1); 
     test1.addHyphen(); 
     out.println(test1); 
     test1.alphabetize(); 
     out.println(test1); 

     WordFun test2 = new WordFun("Computer Science"); 
     out.println(test2); 
     test2.makeUpper(); 
     out.println(test2); 
     test2.addHyphen(); 
     out.println(test2); 
     test2.alphabetize(); 
     out.println(test2); 

     WordFun test3 = new WordFun("Golden Bears"); 
     out.println(test3); 
     test3.makeUpper(); 
     out.println(test3); 
     test3.addHyphen(); 
     out.println(test3); 
     test3.alphabetize(); 
     out.println(test3); 

     WordFun test4 = new WordFun("Upper Arlington"); 
     out.println(test4); 
     test4.makeUpper(); 
     out.println(test4); 
     test4.addHyphen(); 
     out.println(test4); 
     test4.alphabetize(); 
     out.println(test4); 
    } 
} 
+0

「word =」を 'changedWord ='に、 'original'を' word'に置き換えます –

答えて

0

あるコードの下に参照してください。

public void alphabetize() { 
    int loc; 
    loc = word.indexOf(" "); 
    String wordOne = word.substring(0, loc); 
    String wordTwo = word.substring(loc + 1, word.length()); 
    if (wordOne.compareTo(wordTwo) > 0) { 
     changedWord = wordTwo + " " + wordOne; 
    } else { 
     changedWord = wordOne + " " + wordTwo; 
    } 
} 
関連する問題