【发布时间】:2015-03-01 19:58:03
【问题描述】:
我有一个程序,我需要它来询问用户一个词,例如你好,然后要求一个模式,例如2 然后取出该单词并拆分字符并在单词的字母之间放置随机字母,因此在这种情况下,用户模式为3,因此hello 将输出为**h**as**e**rg**l**ty**l**oh**o**。所以我有一部分是我向用户询问一个词,然后我用System.out.print(userWord.charAt(0));溢出了这个词的字符
我只需要知道每次在单词字符之间如何随机生成不同的字母。我的代码生成字母的部分是这样的:
public static void main (String[] args)
{
int cipherchoice = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a number!"));
int z;
String mixedarray1 [] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",};
int x = mixedarray1.length; // determines the length of mixedarray
String[] myarray = new String [cipherchoice]; // create a new arracy to be coded
for (int i = 0; i < cipherchoice; i++) // loop equal size of coded array
{
z = (int)(Math.random()*x); // to create a random index value from mixedarray
myarray[i] = mixedarray1[z]; // assigned myarray a random index value from mixedarray
System.out.print (myarray [i]); // prints results using print (not println)
}
}
所以我的输出是System.out.print(myarray [i]); 那么你如何获得多个不同的输出(例如,如果用户在另一段代码中输入 5 个你将如何获得 5 个不同的随机字母输出? p>
【问题讨论】:
-
你的解决方案有什么问题?
-
“不同的输出”到底是什么意思?永远不要在同一个词中使用相同的字母?如果单词足够大,需要超过 26 个字符怎么办?