0

我在做一个If and 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

}

}
4

1 回答 1

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

:)

于 2013-10-18T01:31:32.747 回答