试着把一个句子分成几个单词,然后交换每个单词的第一个和最后一个字母



有人知道为什么它不起作用吗?

这是主要类的代码:

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;
    }
}

这是我的应用程序类的代码:

import javax.swing.JOptionPane;
public class FirstNLastApp {
    public static void main(String[]args) {
        String sentence = "";
        String[] word = sentence.split(" ");
        String newWord;
        StringBuilder 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();
        sentence=fnl.getSentence();
        JOptionPane.showMessageDialog(null, "The new word is " + sentence);
    }
}

我没有看你的代码,但我会在标题中回答你的问题:

    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的提示:)

我的建议是使用IDE的内置调试器。设置一些断点,并使用单步执行和调试器的变量查看器来观察发生了什么。

我不能给你详细的说明如何做到这一点,因为它取决于你的IDE。但毫无疑问,会有关于如何做到这一点的帮助/教程信息。在那之后,这是一个学习如何为自己做这件事的问题。(你越早学会为自己做越好!)

这是我为Instantable类编写的代码。它用于交换单词的第一个字母和最后一个字母。但后来我的问题是,我需要输入一个句子,并将其应用于句子中的每个单词,所以我离开了这门课,创建了一个名为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;
    }
}

下面是第一节NLast课,学习如何拆分句子。

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);
    }
}

最新更新