[英]How would one be able to randomly print 10 strings with 'n' letters, but each string has the same first letter
我正在尝试制作一个代码,该代码将随机输入,直到找到用户输入的正确单词。 到目前为止,我有以下内容:(我真的是编码新手)。
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);
}
}
}
}
}
有一个困难我过不去。 我想让这段代码更有效率。 一旦它随机运行第一个字符串,我希望它保存正确的字母。 下次打印字符串时,我希望这些字母位于同一位置。 例如:这个词是你好。 计算机输入的第一个字符串是 fstli。 如您所见,倒数第二个字母匹配。 在代码打印的下一个字符串中,我希望 l 位于同一个位置。 如果下一个单词是 Hplli,我希望 H、L 和 L 留在正确的位置。
谢谢你
您必须使用两种不同的方法创建随机String
。 第一种方法创建初始随机String
。 第二种方法将输入的单词与之前的随机String
进行比较。
这是我运行的测试,显示了猜测。 使用两个或三个字母的单词,否则您将生成一个长 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.
我一次只写了一个方法。 通过将代码分解为方法,我可以单独测试每个方法。
这是完整的可运行代码。
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.