samedi 3 janvier 2015

How to get multiple outputs of random letters?

So I have a program and I need it to ask the user for a word e.g. hello and then ask for a pattern e.g. 2 and then take that word and split up the characters and put random letters between the letters of the word so in this case the users pattern was 3 so hello would be outputted as hasergltyloho. So I have the part where I ask the user for a word and I splt up the characters of the word with System.out.print (userWord.charAt(0)); I just need to know how you would generate random different letters each time between the words charactsers. The section of my code that generates the letters is this:


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)
}


}


So my output is System.out.print (myarray [i]); So how do you get multiple different outputs (E.g. if the user enters 5 in another section of code how would you get 5 different outputs of random letters?





Aucun commentaire:

Enregistrer un commentaire