Java 试着把一个句子分成几个单词,然后交换每个单词的第一个和最后一个字母
有人知道为什么不成功吗 以下是主类的代码:Java 试着把一个句子分成几个单词,然后交换每个单词的第一个和最后一个字母,java,Java,有人知道为什么不成功吗 以下是主类的代码: public class FirstNLast { private String word[]; private String sentence = ""; private String newWord; private StringBuilder strBuff; private int len; private char firstLetter; private char lastLetter
public class FirstNLast {
private String word[];
private String sentence = "";
private String newWord;
private StringBuilder strBuff;
private int len;
private char firstLetter;
private char lastLetter;
public FirstNLast(){
word = sentence.split(" ");
newWord = "";
strBuff = new StringBuilder();
len = 0;
firstLetter = ' ';
lastLetter = ' ';
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public void compute(){
len = word.length;
firstLetter=word[0].charAt(0);
lastLetter=word[len-1].charAt(len-1);
for (int i=0; i < word.length; i = i + 1) {
if (word[i].charAt(i)==firstLetter) {
strBuff.append(lastLetter);
}
else if (word[i].charAt(i)==lastLetter) {
strBuff.append(firstLetter);
}
else {
strBuff.append(word[i].charAt(i));
}
newWord = strBuff.toString();
}
}
public String getSentence(){
return sentence;
}
}
我的建议是使用IDE的内置调试器。设置一些断点,并使用单步执行和调试器的变量查看器来观察正在发生的情况
我不能给你详细的说明如何做到这一点,因为它取决于你的IDE。但毫无疑问,将有关于如何做到这一点的帮助/教程信息。在那之后,就是学习如何为自己做这件事。(你越早学会为自己做就越好!)我没有看你的代码,但我会在标题中回答你的问题:
String[] words = sentence.split(" ");
String newSentence = "";
for(String word : words){
char[] letters = word.trim().toCharArray();
char firstChar = letters[0];
letters[0] = letters[letters.length - 1];
letters[letters.length - 1] = firstChar;
newSentence += new String(letters) + " ";
}
return newSentence;
要理解代码不起作用的原因,您应该遵循Stephen C的提示:)以下是我为Instantiable类编写的代码。它用于交换单词的第一个字母和最后一个字母。但我的问题是我需要输入一个句子,并将其应用于句子中的每个单词,所以我离开了这个课程,创建了一个名为FirstNLast的新课程,在这个课程中我研究了如何拆分句子
public class FirstNLastWord {
private String word;
private String newWord;
private StringBuffer strBuff;
private int len;
private char firstLetter;
private char lastLetter;
public FirstNLastWord() {
word = "";
newWord = "";
len = 0;
firstLetter = ' ';
lastLetter = ' ';
}
public void setWord(String word) {
this.word = word;
}
public void compute() {
strBuff = new StringBuffer();
len = word.length();
firstLetter=word.charAt(0);
lastLetter=word.charAt(len-1);
for(int i=0; i < word.length(); i = i + 1) {
if(word.charAt(i)==firstLetter) {
strBuff.append(lastLetter);
}
else if(word.charAt(i)==lastLetter) {
strBuff.append(firstLetter);
}
else {
strBuff.append(word.charAt(i));
}
newWord = strBuff.toString();
}
}
public String getNewWord() {
return newWord;
}
}
import javax.swing.JOptionPane;
public class FirstNLastApp{
public static void main(String[]args){
String sentence="";
String word[]=sentence.split(" ");
String newSentence="";
StringBuffer strBuff;
int len=0;
char firstLetter;
char lastLetter;
FirstNLast fnl = new FirstNLast();
sentence = JOptionPane.showInputDialog(null, "Please enter a sentence");
fnl.setSentence(sentence);
fnl.compute();
newSentence=fnl.getNewSentence();
JOptionPane.showMessageDialog(null, "The new sentence is " + newSentence);
}
}
你能告诉我们什么不可行吗?你遇到的问题在哪里?你预计会发生什么?现在正在发生什么?这个问题看起来是从?但是这次有什么问题吗?编译很好,但没有运行。你的问题和评论就像这样说:“医生,我感觉不舒服。治疗方法是什么?”@user109649你的代码有点混乱,但这是一个很好的实践问题:)
public class FirstNLast{
private String word[];
private String sentence, newWord;
private String newSentence;
private StringBuffer strBuff;
private int len;
private char firstLetter;
private char lastLetter;
private FirstNLastWord fnl;
public FirstNLast(){
sentence = "";
newSentence = "";
strBuff = new StringBuffer();
len = 0;
firstLetter = ' ';
lastLetter = ' ';
fnl=new FirstNLastWord();
}
public void setSentence(String sentence){
this.sentence = sentence;
}
public void compute(){
word = sentence.split(" ");
len = word.length;
for(int i=0; i < word.length; i = i + 1){
fnl.setWord(word[i]);
fnl.compute();
newSentence= newSentence+" "+fnl.getNewWord();
}
}
public String getNewSentence(){
return newSentence;
}
}
import javax.swing.JOptionPane;
public class FirstNLastApp{
public static void main(String[]args){
String sentence="";
String word[]=sentence.split(" ");
String newSentence="";
StringBuffer strBuff;
int len=0;
char firstLetter;
char lastLetter;
FirstNLast fnl = new FirstNLast();
sentence = JOptionPane.showInputDialog(null, "Please enter a sentence");
fnl.setSentence(sentence);
fnl.compute();
newSentence=fnl.getNewSentence();
JOptionPane.showMessageDialog(null, "The new sentence is " + newSentence);
}
}