'How would one be able to randomly print 10 strings with 'n' letters, but each string has the same first letter
I am trying to make a code that will put random inputs until it finds the correct word inputted by the user. So far, I have the following: (I am really new to coding).
import java.util.*;
public class RandomeWords {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
System.out.println("Enter Words");
String words = scanner1.nextLine();
int letters = words.length();
for(int i1 = 1; i1 > 0; i1++) {
if(words.contains(" ")) {
String[] alpha = " abcdefghijklmnopqrstuvwxyz".split("");
StringBuilder sb = new StringBuilder("");
for (int i3 = 0; i3 < letters; i3++) {
sb.append(alpha[(int)(Math.random()*27)]);
}
String finall = sb.toString();
if(!finall.equals(words)) {
System.out.println(finall);
}
if(finall.equals(words)) {
System.out.println(finall);
System.out.println(i1);
System.out.println("it took " +i1 + " tries to randomly find the word.");
System.out.println("It should have taken " +Math.pow((int) 27, letters) +" times, statistacally");
System.out.println("The difference of statistically, and with the simulation is " +(int)Math.abs((int)i1 - (int)Math.pow(27, letters)));
System.exit(0);
}
}
if(!words.contains(" ")){
String[] alpha1 = "abcdefghijklmnopqrstuvwxyz".split("");
StringBuilder sb1 = new StringBuilder("");
for(int i2 = 0; i2 < letters; i2++) {
sb1.append(alpha1[(int)(Math.random()*26)]);
}
String finall1 = sb1.toString();
if(!finall1.equals(words))
System.out.println(finall1);
if(finall1.equals(words)) {
System.out.println(finall1);
System.out.println(i1);
System.out.println("it took " +i1 + " tries to randomly find the word.");
System.out.println("It should have taken " +Math.pow((int) 26, letters) +" times, statistacally");
System.out.println("The difference of statistically, and with the simulation is " +Math.abs((int)i1 - (int)Math.pow(26, letters)));
System.exit(0);
}
}
}
}
}
There is one difficulty that I can't get past. I would like to have this code be more efficient. Once it randomly runs the first string, I would like it to save the correct letters. The next time it prints a string, I would like the letters to be in the same spot. EX: The word is HELLO. the first string the computer inputs is fstli. As you can see, the second to last letter matches up. In the next string the code prints, I would like to have that l be in the same spot. If the next word is Hplli, I would like the H, L, and L to stay in the correct spot.
Thank You
Solution 1:[1]
You have to create the random String using two different methods. The first method creates the initial random String. The second method compares the input words with the previous random String.
Here's a test I ran that shows the guesses. Use a two or three letter word or else you'll generate a long output.
The computer will randomly guess your words.
Enter one or more words. Just press enter to quit.
but
Guess 1 is: ist
Guess 2 is: wgt
Guess 3 is: v t
Guess 4 is: cnt
Guess 5 is: eut
Guess 6 is: nut
Guess 7 is: eut
Guess 8 is: ut
Guess 9 is: gut
Guess 10 is: sut
Guess 11 is: tut
Guess 12 is: eut
Guess 13 is: hut
Guess 14 is: ut
Guess 15 is: aut
Guess 16 is: mut
Guess 17 is: aut
Guess 18 is: uut
Guess 19 is: wut
Guess 20 is: tut
Guess 21 is: yut
Guess 22 is: gut
Guess 23 is: but
The computer will randomly guess your words.
Enter one or more words. Just press enter to quit.
I wrote this code one method at a time. By breaking the code up into methods, I could test each method individually.
Here's the complete runnable code.
import java.util.Random;
import java.util.Scanner;
public class RandomWords {
public static void main(String[] args) {
RandomWords rw = new RandomWords();
Scanner scanner = new Scanner(System.in);
rw.processWords(scanner);
scanner.close();
}
private Random random;
public void processWords(Scanner scanner) {
this.random = new Random();
String line;
do {
System.out.println("The computer will randomly guess your words.");
System.out.println("Enter one or more words. Just press enter to quit.");
line = scanner.nextLine().trim().toLowerCase();
if (!line.isEmpty()) {
processGuesses(line);
}
} while (!line.isEmpty());
}
private void processGuesses(String line) {
int count = 0;
String randomString = generateRandomString(line);
while (!randomString.equals(line)) {
System.out.println("Guess " + ++count + " is: " + randomString);
if (!randomString.equals(line)) {
randomString = generateRandomString(line, randomString);
}
}
System.out.println("Guess " + ++count + " is: " + randomString);
}
private String generateRandomString(String line, String randomString) {
char[] alphabet = generateAlphabet();
String output = "";
char[] lineCharacters = line.toCharArray();
char[] randomCharacters = randomString.toCharArray();
for (int index = 0; index < line.length(); index++) {
if (lineCharacters[index] == randomCharacters[index]) {
output += lineCharacters[index];
} else {
int sub = random.nextInt(alphabet.length);
output += alphabet[sub];
}
}
return output;
}
private String generateRandomString(String line) {
char[] alphabet = generateAlphabet();
String output = "";
for (int index = 0; index < line.length(); index++) {
int sub = random.nextInt(alphabet.length);
output += alphabet[sub];
}
return output;
}
private char[] generateAlphabet() {
return " abcdefghijklmnopqrstuvwxyz".toCharArray();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gilbert Le Blanc |
