【问题标题】:how can I code the rest of this program so that the user guesses are marked if true?我如何编码该程序的其余部分,以便用户猜测是否为真?
【发布时间】:2023-03-25 02:40:01
【问题描述】:

我正在制作这个程序,它会生成这些字符 CMFBOL 的随机 4 个字符序列。我需要这个程序来接受用户输入,检查他们的猜测是否在正确的位置,作为字母 CMFBOL 中随机生成的序列,并为正确位置的字符返回 1,在错误位置返回 0。这是我到目前为止所拥有的。为了进一步详细说明我的程序将做什么,随机生成器生成一个序列,例如 CFBM,它将该字符序列存储在一个字符串中并提示用户猜测这些字符的序列。如果他们的猜测字母在正确的位置,它会打印一个 1。如果他们的猜测包含正确的字符但在错误的位置,它会打印 0。例如,随机生成器生成 CMFB,并提示用户猜测。用户输入 CLBO,程序输出 10。1 表示 C 位置正确,0 表示用户猜测 B 但位置不正确。

else if (anotherScanner.hasNext("c|C"))
                    { 

                        c = anotherScanner.next();
                        //usersSelection = true;
                        System.out.println("");
                        System.out.println("COLOR CHALLENGE");
                        System.out.println("Valid color letters are:");
                        System.out.println("CMFBOL for Cyan Magenta Fushia BabyBlue Orange Lemon");

                        System.out.println("1) Guess:");
                        Random randomss = new Random();
                        String acronColor = "CMFBOL";
                        final int length = 4;

                        char let1 = acronColor.charAt(randomss.nextInt(length));
                        char let2 = acronColor.charAt(randomss.nextInt(length));
                        char let3 = acronColor.charAt(randomss.nextInt(length));
                        char let4 = acronColor.charAt(randomss.nextInt(length));

                        String acronColorRand = (""+let1+let2+let3+let4);
                        }

【问题讨论】:

  • 没有得到您的问题?
  • 好的,那么,这个程序是做什么的,它与您的期望有何不同?
  • 还是不明白你的问题?你是什​​么意思-如果为真,所有猜测都会被标记?

标签: java loops random sequence


【解决方案1】:

你看起来像个新手,所以这里有一些关于你的代码应该是什么样子的逻辑。 尝试并了解它的作用并根据需要改进它。

    public static void main(String ... args){
    ........
    //some code here hopefully you alreday have 
    if (anotherScanner.hasNext("c|C"))
    { 

        c = anotherScanner.next();
        //usersSelection = true;
        String acronColor = "CMFBOL";
        System.out.println("COLOR CHALLENGE\n " +
                "Valid color letters are:\n" + acronColor +
                " for Cyan Magenta Fushia BabyBlue Orange Lemon");
        System.out.println("1) Guess:");
        Random randomss = new Random();
        final int length = 4;
        StringBuilder s = new StringBuilder();
        for(int i = 0; i < 4; i++){
            s.append(acronColor.charAt(randomss.nextInt(length)));
        }
        String acronColorRand = s.toString().toUpperCase();
        System.out.println(eval(guess, acronColorRand));
        }
}


private String eval(String guess, String random){
    StringBuilder s = new StringBuilder();
    guess = guess.toUpperCase();
    for(int i = 0; i < random.length(); i++){
        char r = (guess.charAt(i) == random.charAt(i)) ? '1' : '0';
        s.append(r);
    }
    return s.toString();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多