用java按字母顺序排列单词
这是我的程序,我不确定它出了什么问题。如果我删除了字母排序的方法,它完全可以正常工作,有人能告诉我这个方法有什么问题以及如何修复它吗。第一部分我应该按单词出现的样子打印,第二部分我应该大写,然后第三部分我应该添加连字符并删除单词之间的空格,最后一部分我应该按字母顺序排列用java按字母顺序排列单词,java,string,Java,String,这是我的程序,我不确定它出了什么问题。如果我删除了字母排序的方法,它完全可以正常工作,有人能告诉我这个方法有什么问题以及如何修复它吗。第一部分我应该按单词出现的样子打印,第二部分我应该大写,然后第三部分我应该添加连字符并删除单词之间的空格,最后一部分我应该按字母顺序排列 import static java.lang.System.*; public class WordFun { private String word, loc, changedWord; public
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);
}
}
您的
字母排序()方法中缺少属性original
请参阅下面的代码
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;
}
}
用changedWord=
替换word=
,用word