如何将字母从单词的第一个字母移到单词的后面?然后在末尾添加“ay”

时间:2013-10-18 01:12:13

标签: java string substring

我正在做一个If和else的项目,这个项目要求你接受用户输入,然后将他们输入的单词的第一个字母移动到单词的最后一个字母。 问题是我不知道用户的最后号码是什么。

我想为此使用substring方法。 我不知道如何将一个字母从单词的开头移到单词的最后一个字母,因为我不知道单词的最后一个字母是什么(用户可以输入任何内容)

我开始这样做,但我根本没有进展,因为我不知道用户输入的最后一个字母是什么。

import javax.swing.*;
public class PigLatinDriver {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String word = JOptionPane.showInputDialog("Enter the word to be translated into pig Latin.");


    boolean consonant =false;

    If(word.substring(0)== "b");{
        word.substring(0).replace("b"," ");

    }
    if(consonant==true){

    }

    //perform the logic to translate to piglatin
    //Words that begin with a consonant move the first letter to the end and then add ay

    // vowels just add ay
    // The exception being when you use the word "Small" with an upper case, then you make no change.

    String translation = "";

    JOptionPane.showMessageDialog(null, translation);
}

private static void If(boolean b) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

您可以使用String API中的concat()方法简单地连接字符串。或者你可以简单地使用'+'运算符作为字符串。

试试这个:

Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); //get input from user

char firstLetter = input.charAt(0); //get the first letter
input = input.substring(1); //remove the first letter from the input string
input = input + firstLetter + "ay"; //add first letter and "ay" to end of input string

:)