编写一个程序,从用户输入中收集一个短语,并在每个单词中随机移动字母,而不复制任何字母,字符或空格。 [英] Write a program that will collect a phrase from user input and randomly move letters around in each word, without duplicating any letters, characters, or spaces.
本文介绍了编写一个程序,从用户输入中收集一个短语,并在每个单词中随机移动字母,而不复制任何字母,字符或空格。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例提供,请帮助我。
WordScramble(常规示例)
输入您要编码的短语:
你好,我喜欢长周末
新的炒作短语:
lndoi,v hwds lleo olneegee
WordScramble(额外信用示例)
输入您要编码的短语:
你好,我喜欢长周末
新的炒作短语:
Leloh,我看到了你的朋友
我尝试了什么:
Example provided, please help me.
WordScramble (regular example)
Enter your phrase to be coded:
Hello, I love long weekends
New Scrambled phrase:
lndoi, v hwds lleo olneegee
WordScramble (extra credit example)
Enter your phrase to be coded:
Hello, I love long weekends
New Scrambled phrase:
Leloh, I ovle glon seenkedw
What I have tried:
public class wordScramble
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Enter phrase");
String str= input.nextLine();
int x = str.length();
int counter = 0;
int num = (int)Math.random();
StringBuilder sb = new StringBuilder(str);
while (str.length() > 0 )
{
String word = str.substring(0,counter++);
sb.deleteCharAt(num);
String result = sb.toString();
System.out.println(result);
}
}
}
推荐答案
参见 String.split(Java Platform SE 7) [< a href =https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)target =_ blanktitle =New Window > ^ ]这将打破这句话。然后,您可以在重新组合短语之前单独调整每个单词的字母。
See String.split (Java Platform SE 7 )[^] which will break up the phrase. you can then adjust the letters of each word independently before reassembling the phrase.
这篇关于编写一个程序,从用户输入中收集一个短语,并在每个单词中随机移动字母,而不复制任何字母,字符或空格。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文