Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,将用户输入的单词的第一个字母重复添加到末尾_Java - Fatal编程技术网

在java中,将用户输入的单词的第一个字母重复添加到末尾

在java中,将用户输入的单词的第一个字母重复添加到末尾,java,Java,因此,我试图获取用户输入的单词的第一个字母,并将其添加到单词的末尾(这将是一个密码键),直到消息完成 例如: 密码关键字 留言-请帮忙 密码密钥将转换为ascii数除以13,因此w、o、r、d将转换为119111114100,然后转换为9,9,9,8 因此,我的问题是如何将代码转换为能够获取用户输入的密码密钥的第一个字母并将其添加到末尾,直到消息结束 因此,假设密码密钥为word,消息为请帮助消息有11个字符,因此密码密钥应将第一个字母放在后面十次 例如:单词、ordw、rdow、dwor、单词

因此,我试图获取用户输入的单词的第一个字母,并将其添加到单词的末尾(这将是一个密码键),直到消息完成

例如:

密码关键字

留言-请帮忙

密码密钥将转换为ascii数除以13,因此w、o、r、d将转换为119111114100,然后转换为9,9,9,8

因此,我的问题是如何将代码转换为能够获取用户输入的密码密钥的第一个字母并将其添加到末尾,直到消息结束

因此,假设密码密钥为word,消息为请帮助消息有11个字符,因此密码密钥应将第一个字母放在后面十次
例如:单词、ordw、rdow、dwor、单词、ordw等

import java.util.Scanner;

public class swap {

    public static void main(String [] args) {

        System.out.println("Enter the word you want to flip");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine(); //get input from user

    for (;;) {
        char firstLetter = input.charAt(0); //get the first letter
        input = input.substring(1); //remove the first letter from the input string
        input = input + firstLetter; //add first letter to end of input string
        System.out.println("the word you entered flipped is " + input);
        break;
        }
    }
}

您的要求很奇怪,因为将单词的每个字母的第一个字母发送到单词的末尾将给出首字母作为最终结果

如果您只想反转一个单词,那么可以使用
StringBuilder
并调用
reverse
方法:

StringBuilder hello = new StringBuilder("hello");
hello.reverse();
在本例中,hello将等于olleh。如果您不想使用StringBuilder,并且需要编写算法,因为它是家庭作业或w/e,那么请查看以下线程:

编辑:要回答您的评论,这里有一个10个字符的示例

StringBuilder foo = new StringBuilder("somerandom(actuallynotreallyrandom)characters");
for(int i = 0; i < 10; i++)
{
    foo.append(foo.charAt(0));
    foo.deleteCharAt(0);
}
StringBuilder foo=新的StringBuilder(“一些随机(实际为非随机)字符”);
对于(int i=0;i<10;i++)
{
foo.append(foo.charAt(0));
foo.deleteCharAt(0);
}

如果我对问题的理解是正确的,对于作为输入的
word
和作为消息的
hello world
,我们将得到一个列表
[ordw,rdwo,dwor,word,ordw,rdwo,dwor,word,ordw,rdwo]
作为输出

String word = "word";
String message = "hello world";
int numShifts = message.length() - 1;
List<String> newWords = new ArrayList<String>();
int wordLength = word.length();
String tempWord = word;
for (int j = 0; j < numShifts ; j++) {
    StringBuffer sb = new StringBuffer();
    String[] charList = tempWord.split("");
    for (int i = 1; i < charList.length; i++) {
        sb.append(charList[(i % wordLength) + 1]);
    }
    tempWord = sb.toString();
    newWords.add(tempWord);
}
System.out.println(newWords);
String word=“word”;
String message=“hello world”;
int numshift=message.length()-1;
List newWords=new ArrayList();
int wordLength=word.length();
字符串tempWord=单词;
对于(int j=0;j
作为旁注,您打算如何将其转换回去?也许只有我一个人,但这个问题对我来说似乎并不清楚。您是否计划将每个输入的单词的第一个
字符
移动到末尾一次(例如
hello
elloh
)?或者你是想把每个词都倒转过来(例如,
hello
olleh
)?你的句子“那么,我的问题……结束了”没有意义。编辑以澄清,提供示例。如果你将字母除以13,那么你将只保留从A到M的信息。我不理解这里的概念。除非这与凯撒·塞弗无关。虽然我不确定这和密码有什么关系。我真的很困惑。我使用的密钥只是一个示例,用户将输入他们的密钥。密码密钥转换为ascii数字并除以13后,它将被集成到一个程序中,该程序将根据除以的数字移动用户将输入的消息。因此,我在我的问题中添加了更多信息“假设……十次”@RStone对此有任何反馈吗?那么Jean François我如何将其集成到我的代码中,以便不将i<10设置为10,而是设置为用户需要的字符数inputs@RStone只需在
i
而不是
i<10
当我尝试在textpad中使用你的代码时,它会说找不到。当我尝试编译文件时添加:newWords.add(tempWord);您将需要导入java.util.ArrayList和java.util.list。我建议您使用像EclipseUr Welcome@RStone这样的IDE。希望这能引导你度过难关。