猜谜游戏Java

猜谜游戏Java,java,Java,我正在写一个游戏,让用户猜测随机生成的数字。最后,它将显示正确的总数以及这些正确数字的总和。但是,我无法显示用户拥有的正确数字的正确和。有人能帮我吗?谢谢大家! public static void main(String[] args) { Random rng = new Random(); Scanner consoleScanner = new Scanner(System.in); String inputString; int answer = rng

我正在写一个游戏,让用户猜测随机生成的数字。最后,它将显示正确的总数以及这些正确数字的总和。但是,我无法显示用户拥有的正确数字的正确和。有人能帮我吗?谢谢大家!

public static void main(String[] args) {
    Random rng = new Random();
    Scanner consoleScanner = new Scanner(System.in);
    String inputString;
    int answer = rng.nextInt(90000) + 10000, sum, numberCorrect;
    System.out.print("I have randomly chosen a 5-digit code for you to guess.\n"
            + "Each time you guess, I will tell you how many digits are correct and the sum of the digits that are correct.\n"
            + "For example, if the number is \"68420\" and you guess 12468, I will respond:\n"
            + "Number of Digits Correct: 1\n" + "Sum of Digits Correct   : 4\n"
            + "From deduction, you will know the 4 was correct in the guess.\n\n"
            + "Now its your turn..................................................................\n" + "answer = "
            + answer);
    do {
        System.out.print("\nPlease enter a 5-digit code (your guess): ");
        inputString = consoleScanner.nextLine();
        numberCorrect = 0;
        sum = 0;
        if (inputString.length() != 5) {
            System.out.println("Please enter 5-digit code only.");
            System.exit(0);
        }
        for (int i = 0; i < 5; i++) {
            String answerString = String.valueOf(answer);
            if (inputString.charAt(i) == answerString.charAt(i)) {
                numberCorrect++;
                char digit = answerString.charAt(i);
                sum += digit;
            }
        }
        System.out.println("Number of Digits Correct: " + numberCorrect + "\nSum of Digits Correct:    " + sum);
    }
    while (numberCorrect < 5);
    System.out.println("****HOORAY!  You solved it.  You are so smart****");
}
publicstaticvoidmain(字符串[]args){
随机rng=新随机();
扫描仪控制台扫描仪=新扫描仪(System.in);
字符串输入字符串;
int answer=rng.nextInt(90000)+10000,总和,数字正确;
System.out.print(“我随机选择了一个5位代码供您猜测。\n”
+每次你猜,我都会告诉你有多少数字是正确的,以及正确数字的总和。\n
+例如,如果号码是“68420”,而您猜是12468,我将回答:\n
+“正确的位数:1\n”+“正确的位数总和:4\n”
+通过推断,您将知道猜测中的4是正确的。\n\n
+“现在轮到你了……”\n“+”答案=”
+答复);
做{
System.out.print(“\n请输入一个5位代码(您的猜测):”;
inputString=consoleScanner.nextLine();
numberCorrect=0;
总和=0;
如果(inputString.length()!=5){
System.out.println(“请仅输入5位代码”);
系统出口(0);
}
对于(int i=0;i<5;i++){
String answerString=String.valueOf(answer);
if(inputString.charAt(i)=answerString.charAt(i)){
numberCorrect++;
字符数字=应答字符串.charAt(i);
总和+=位数;
}
}
System.out.println(“正确的位数:+numberCorrect+”\n正确的位数:+sum”);
}
而(正确数<5);
System.out.println(“****万岁!你解决了它。你太聪明了****”;
}

您必须将字符转换为数值:

sum += Character.getNumericValue(digit);

sum变量是整数,您正试图将其添加到char(数字)中。您需要将字符转换为有效的int,然后将其添加到sum中

以下参考资料可能会有所帮助


对不起,StackOverflow不是这样工作的。“这是我的一堆代码,请帮我调试一下”这样的问题被认为是离题的。请访问并阅读了解更多信息,尤其是阅读您的数字变量是char类型。因此,当我们得到一个“0”时,它实际上是48的整数值(ASCII表),您需要将字符转换为int。非常感谢!!非常感谢你!!这些链接真的很有用!