怎么解决int无法转换为int[]的问题?

huangapple go评论48阅读模式
英文:

How do I solve int cannot be converted to int[]?

问题

我创建了一个名为Mastermind的Java程序,用于一个名为Mastermind的棋盘游戏。我创建了Mastermind类并在我的主方法中调用它。我尝试从我创建的数组中输入数字,但在我尝试调用Mastermind时出现了找不到符号的错误。之后,我将它换成了其他内容,并将其声明为guessResult = Mastermind.attempt(guess[2])。现在我遇到了一个int无法转换为int[]的错误,我该如何解决这个问题?

英文:

I created a java program for a board game called Mastermind. I have created the class called Mastermind and called it in the my main method. I tried inputting numbers from the array I created but it giving me an error of it unable to find the symbol when I tried calling Mastermind. I switched it something else after and stated it as guessResult = Mastermind.attempt(guess[2]); . Now I have an error of int cannot be converted to int[]?, how do I solve this?

import java.util.Scanner;

public class MastermindGame
{
    public static void main(String[] args)
    {
        //game instructions
        System.out.print("Welcome to Mastermind!");
        System.out.print("The computer will generate a passcode with your specified number of digits.");
        System.out.print("Your job is to break that code!");
        System.out.print("After each guess, the computer will tell you how many correct digits are in the correct position.");
        System.out.print("It will do this by printing one X for each correct digit.");
        System.out.print("It does not tell you which digit is correct.");
        System.out.println("If you do not see any Xs print, none of your digits were correct.");
        System.out.println("GOOD LUCK!");
        
        //ask user to determine how many digits the code will be
        System.out.println("How many digits would you like the code to be?");
        //enable input from the user
        Scanner in = new Scanner(System.in);
        //read the integer the user types
        int length = in.nextInt();
        //create the new game with the indicated number of digits
        Mastermind game = new Mastermind(length);
        
        //ssshh!! cheating so we can see if this works!
        System.out.println("Shhh! Just so we know if our program works, the code is ");
        game.printCode();
        
        //set up an array to hold the user's guesses
        int[] guess = {1, 2, 3, 4};
        
        //set up a String to display the result of the guess
        String guessResult = " ";
        
                
        //allow the user 4 tries to break the code
        for (int j = 0; j < 4; j++)
        {
            //tell the user to take a guess
            System.out.println("Gander a guess");
            //loops as many digits (length) as they indicated the code should be
            for (int i = 0; i < 4; i++)
            {
                //ask the user to input their guess for this digit
                System.out.println(" What is the next digit for the code");
                //store their guess in the guess array
                guess[i] = in.nextInt();
            }
            //call the method in the Mastermind class that checks their guess against the code
            guessResult = Mastermind(2);
            //it will print out as many Xs as correct digits, order irrelevant
            System.out.println ( guessResult);
            
            //if the user guess, game over so end the main method with "return"
            if (guessResult.equals("YOU WIN!!"))
                return;
        }
        //if we get outside the loop, user wasn't able to guess, "game over"
        System.out.println("~~~GAME OVER~~~");
    }
}

class Mastermind
{
    private int[] code;
    
    //overloaded constructor
    public Mastermind(int length)
    {
        //get the length of the array to the number of digits the user requested
        code = new int[length];
        
        //walk through the code
        for (int i = 0; i < code.length; i++)
        {
            //add a random digit (0-2 in this case) to the code
            code[i] =  (int)(Math.random()*3 + 0);
        }
    }
    
    //method to check the user's guess against the code
    public String attempt(int[] guess)
    {
        String response = "";
        boolean verify = true;
        
        //walk through the guess array
        for (int k = 4; k < 4; k++)
        {
            //compare guess array to code
            //if digits are the same, add an X to response string
            if (code == guess)
               response += "X";
            //if digits are not equal, set verify to false because the guess is not the code
            else
                verify = false;
        }
        
        //if verify was never set to false, then the user guessed correctly so return YOU WIN
        if ( guess== code)
            return "YOU WIN!!";
        
        //if code gets here, user did not guess correctly, return the String with Xs for correct number of digits.
        return "X";
    }
    
    //created this method so we can cheat to check that our game works
    public void printCode()
    {
        String answer = "";
        //walk through code, concatenating the digit to the answer string
        for (int i = 0; i < code.length; i++)
        {
            answer += code[i] + " ";
        }
        //print the code
        System.out.println(answer);
    }
}

答案1

得分: 0

以下是您代码的翻译版本:

import java.util.Arrays;
import java.util.Scanner;

public class MastermindGame {
    public static void main(String[] args) {
        // 游戏说明
        System.out.println("欢迎来到猜数字游戏!");
        System.out.println("计算机将生成一个包含您指定位数的密码。");
        System.out.println("您的任务是破解这个密码!");
        System.out.println("在每次猜测后,计算机将告诉您有多少个正确的数字位于正确的位置。");
        System.out.println("它将通过打印一个X来表示每个正确的数字。");
        System.out.println("但它不会告诉您哪个数字是正确的。");
        System.out.println("如果您没有看到任何X,表示您的猜测中没有任何正确的数字。");
        System.out.println("祝您好运!");

        // 询问用户确定密码有多少位
        System.out.println("您想要密码有多少位?");
        // 启用用户输入
        Scanner in = new Scanner(System.in);
        // 读取用户输入的整数
        int length = in.nextInt();
        // 使用指定位数创建新游戏
        Mastermind game = new Mastermind(length);

        // 嘘!为了测试我们的程序是否有效,密码是:
        System.out.print("嘘!只是为了测试,密码是:");
        game.printCode();

        // 准备一个数组来存储用户的猜测
        int[] guess = new int[length];

        // 允许用户尝试4次来破解密码
        for (int j = 0; j < 4; j++) {
            // 告诉用户进行一次猜测
            System.out.println("进行一次猜测");
            // 循环与密码位数相同的次数
            for (int i = 0; i < length; i++) {
                // 要求用户输入他们猜测的数字
                System.out.print("请输入密码的下一个数字:");
                // 将他们的猜测存储在猜测数组中
                guess[i] = in.nextInt();
            }

            // 调用Mastermind类中检查猜测是否正确的方法
            int guessResult = game.attempt(guess);
            if (guessResult < 0) {
                System.out.println("您赢了!");
                return;
            } else {
                System.out.print(" ");
                for (int i = 0; i < guessResult; i++)
                    System.out.print("X");
                System.out.print("\n\n");
            }
        }
        // 如果退出循环,表示用户无法破解密码,游戏结束
        System.out.println("~~~游戏结束~~~");
    }
}

class Mastermind {
    private final int[] code;

    // 重载构造函数
    public Mastermind(int length) {
        // 根据用户请求的位数创建数组
        code = new int[length];

        // 遍历数组
        for (int i = 0; i < code.length; i++) {
            // 向密码添加一个随机数字(在0到2之间)
            code[i] = (int) (Math.random() * 3 + 0);
        }
    }

    // 用于检查用户猜测是否正确的方法
    public int attempt(int[] guess) {
        if (Arrays.equals(guess, code)) {
            // 如果玩家赢了,返回-1
            return -1;
        } else {
            int correctDigits = 0;
            for (int i = 0; i < code.length; i++) {
                if (code[i] == guess[i]) {
                    correctDigits++;
                }
            }
            return correctDigits;
        }
    }

    // 创建此方法,以便我们可以检查我们的游戏是否有效
    public void printCode() {
        StringBuilder answer = new StringBuilder();
        // 遍历密码,将数字连接到答案字符串中
        for (int j : code) {
            answer.append(j);
            answer.append(" ");
        }
        // 删除最后的空格
        String answerString = answer.substring(0, answer.length() - 1);
        // 打印密码
        System.out.println(answerString);
    }
}
英文:

Here is a functional version of your code:

import java.util.Arrays;
import java.util.Scanner;
public class MastermindGame {
public static void main(String[] args) {
// game instructions
System.out.println(&quot;Welcome to Mastermind!&quot;);
System.out.println(&quot;The computer will generate a passcode with your specified number of digits.&quot;);
System.out.println(&quot;Your job is to break that code!&quot;);
System.out.println(&quot;After each guess, the computer will tell you how many correct digits are in the correct position.&quot;);
System.out.println(&quot;It will do this by printing one X for each correct digit.&quot;);
System.out.println(&quot;It does not tell you which digit is correct.&quot;);
System.out.println(&quot;If you do not see any Xs print, none of your digits were correct.&quot;);
System.out.println(&quot;GOOD LUCK!&quot;);
// ask user to determine how many digits the code will be
System.out.println(&quot;How many digits would you like the code to be?&quot;);
// enable input from the user
Scanner in = new Scanner(System.in);
// read the integer the user types
int length = in.nextInt();
// create the new game with the indicated number of digits
Mastermind game = new Mastermind(length);
// ssshh!! cheating so we can see if this works!
System.out.print(&quot;Shhh! Just so we know if our program works, the code is &quot;);
game.printCode();
// set up an array to hold the user&#39;s guesses
int[] guess = new int[length];
// allow the user 4 tries to break the code
for (int j = 0; j &lt; 4; j++) {
// tell the user to take a guess
System.out.println(&quot;Gander a guess&quot;);
// loops as many digits (length) as they indicated the code should be
for (int i = 0; i &lt; length; i++) {
// ask the user to input their guess for this digit
System.out.print(&quot; What is the next digit for the code?\n  &quot;);
// store their guess in the guess array
guess[i] = in.nextInt();
}
// call the method in the Mastermind class that checks their guess against the code
int guessResult = game.attempt(guess);
if (guessResult &lt; 0) {
System.out.println(&quot;YOU WIN!!&quot;);
return;
} else {
System.out.print(&quot; &quot;);
for (int i = 0; i &lt; guessResult; i++)
System.out.print(&quot;X&quot;);
System.out.print(&quot;\n\n&quot;);
}
}
// if we get outside the loop, user wasn&#39;t able to guess, &quot;game over&quot;
System.out.println(&quot;~~~GAME OVER~~~&quot;);
}
}
class Mastermind {
private final int[] code;
// overloaded constructor
public Mastermind(int length) {
//get the length of the array to the number of digits the user requested
code = new int[length];
// walk through the code
for (int i = 0; i &lt; code.length; i++) {
//add a random digit (0-2 in this case) to the code
code[i] = (int) (Math.random() * 3 + 0);
}
}
// method to check the user&#39;s guess against the code
public int attempt(int[] guess) {
if (Arrays.equals(guess, code)) {
// Return -1 if the player win
return -1;
} else {
int correctDigits = 0;
for (int i = 0; i &lt; code.length; i++) {
if (code[i] == guess[i]) {
correctDigits++;
}
}
return correctDigits;
}
}
// created this method, so we can cheat to check that our game works
public void printCode() {
StringBuilder answer = new StringBuilder();
// walk through code, concatenating the digit to the answer string
for (int j : code) {
answer.append(j);
answer.append(&quot; &quot;);
}
// Remove the last space
String answerString = answer.substring(0, answer.length() - 1);
// print the code
System.out.println(answerString);
}
}

huangapple
  • 本文由 发表于 2023年4月20日 01:16:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定