这是我的程序,我不确定它有什么问题。如果我删除Alphabetizing方法它完全正常,有人可以告诉我这个方法有什么问题以及如何解决它。第一部分我应该打印它们出现的单词,第二部分我应该将它们大写,然后第三部分我应该添加一个连字符并删除单词之间的空格,最后是我认为的最后一部分按字母顺序划分它们。
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;
}
}
这是跑步者计划
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 :(得分:0)
original
方法中缺少属性alphabetize()
参考下面的代码。
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;
}
}