簡體   English   中英

如何隨機打印 10 個帶有“n”個字母的字符串,但每個字符串都有相同的第一個字母

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM