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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19439969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:10:15  来源:igfitidea点击:

How can I move a Letter from the first letter of a word to the back of the word?and then adding "ay" to the end

javastringsubstring

提问by

I'm doing a project of If and else,This project requires you to take a user input, and then move the first letter of the word they entered to the last letter of the word. The problem is I don't know what the last number of the user is.

我在做一个If and else的项目,这个项目需要你拿一个用户输入,然后把他们输入的单词的第一个字母移到单词的最后一个字母。问题是我不知道用户的最后一个号码是多少。

I want to use the substring method for this. I don't know how to move a letter from the beggining of the word to the last letter of the word , because I don't know what is the last letter of the word(the user can enter anything)

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

I started doing it but I have no progress at all, because I don't know what the last letter is of the users input.

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

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

}

}

采纳答案by CodingBird

you can simply concat the string using concat() method from String API. OR you can just simply use the the '+' operator for string.

您可以使用 String API 中的 concat() 方法简单地连接字符串。或者,您可以简单地将“+”运算符用于字符串。

Try this out:

试试这个:

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

:)

:)