[英]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.计算机输入的第一个字符串是 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.在代码打印的下一个字符串中,我希望 l 位于同一个位置。 If the next word is Hplli, I would like the H, L, and L to stay in the correct spot.如果下一个单词是 Hplli,我希望 H、L 和 L 留在正确的位置。
Thank You谢谢你
You have to create the random String
using two different methods.您必须使用两种不同的方法创建随机String
。 The first method creates the initial random String
.第一种方法创建初始随机String
。 The second method compares the input words with the previous random String
.第二种方法将输入的单词与之前的随机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.使用两个或三个字母的单词,否则您将生成一个长 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.