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

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

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?

  1. import java.util.Scanner;
  2. public class MastermindGame
  3. {
  4. public static void main(String[] args)
  5. {
  6. //game instructions
  7. System.out.print("Welcome to Mastermind!");
  8. System.out.print("The computer will generate a passcode with your specified number of digits.");
  9. System.out.print("Your job is to break that code!");
  10. System.out.print("After each guess, the computer will tell you how many correct digits are in the correct position.");
  11. System.out.print("It will do this by printing one X for each correct digit.");
  12. System.out.print("It does not tell you which digit is correct.");
  13. System.out.println("If you do not see any Xs print, none of your digits were correct.");
  14. System.out.println("GOOD LUCK!");
  15. //ask user to determine how many digits the code will be
  16. System.out.println("How many digits would you like the code to be?");
  17. //enable input from the user
  18. Scanner in = new Scanner(System.in);
  19. //read the integer the user types
  20. int length = in.nextInt();
  21. //create the new game with the indicated number of digits
  22. Mastermind game = new Mastermind(length);
  23. //ssshh!! cheating so we can see if this works!
  24. System.out.println("Shhh! Just so we know if our program works, the code is ");
  25. game.printCode();
  26. //set up an array to hold the user's guesses
  27. int[] guess = {1, 2, 3, 4};
  28. //set up a String to display the result of the guess
  29. String guessResult = " ";
  30. //allow the user 4 tries to break the code
  31. for (int j = 0; j < 4; j++)
  32. {
  33. //tell the user to take a guess
  34. System.out.println("Gander a guess");
  35. //loops as many digits (length) as they indicated the code should be
  36. for (int i = 0; i < 4; i++)
  37. {
  38. //ask the user to input their guess for this digit
  39. System.out.println(" What is the next digit for the code");
  40. //store their guess in the guess array
  41. guess[i] = in.nextInt();
  42. }
  43. //call the method in the Mastermind class that checks their guess against the code
  44. guessResult = Mastermind(2);
  45. //it will print out as many Xs as correct digits, order irrelevant
  46. System.out.println ( guessResult);
  47. //if the user guess, game over so end the main method with "return"
  48. if (guessResult.equals("YOU WIN!!"))
  49. return;
  50. }
  51. //if we get outside the loop, user wasn't able to guess, "game over"
  52. System.out.println("~~~GAME OVER~~~");
  53. }
  54. }
  55. class Mastermind
  56. {
  57. private int[] code;
  58. //overloaded constructor
  59. public Mastermind(int length)
  60. {
  61. //get the length of the array to the number of digits the user requested
  62. code = new int[length];
  63. //walk through the code
  64. for (int i = 0; i < code.length; i++)
  65. {
  66. //add a random digit (0-2 in this case) to the code
  67. code[i] = (int)(Math.random()*3 + 0);
  68. }
  69. }
  70. //method to check the user's guess against the code
  71. public String attempt(int[] guess)
  72. {
  73. String response = "";
  74. boolean verify = true;
  75. //walk through the guess array
  76. for (int k = 4; k < 4; k++)
  77. {
  78. //compare guess array to code
  79. //if digits are the same, add an X to response string
  80. if (code == guess)
  81. response += "X";
  82. //if digits are not equal, set verify to false because the guess is not the code
  83. else
  84. verify = false;
  85. }
  86. //if verify was never set to false, then the user guessed correctly so return YOU WIN
  87. if ( guess== code)
  88. return "YOU WIN!!";
  89. //if code gets here, user did not guess correctly, return the String with Xs for correct number of digits.
  90. return "X";
  91. }
  92. //created this method so we can cheat to check that our game works
  93. public void printCode()
  94. {
  95. String answer = "";
  96. //walk through code, concatenating the digit to the answer string
  97. for (int i = 0; i < code.length; i++)
  98. {
  99. answer += code[i] + " ";
  100. }
  101. //print the code
  102. System.out.println(answer);
  103. }
  104. }

答案1

得分: 0

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

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class MastermindGame {
  4. public static void main(String[] args) {
  5. // 游戏说明
  6. System.out.println("欢迎来到猜数字游戏!");
  7. System.out.println("计算机将生成一个包含您指定位数的密码。");
  8. System.out.println("您的任务是破解这个密码!");
  9. System.out.println("在每次猜测后,计算机将告诉您有多少个正确的数字位于正确的位置。");
  10. System.out.println("它将通过打印一个X来表示每个正确的数字。");
  11. System.out.println("但它不会告诉您哪个数字是正确的。");
  12. System.out.println("如果您没有看到任何X,表示您的猜测中没有任何正确的数字。");
  13. System.out.println("祝您好运!");
  14. // 询问用户确定密码有多少位
  15. System.out.println("您想要密码有多少位?");
  16. // 启用用户输入
  17. Scanner in = new Scanner(System.in);
  18. // 读取用户输入的整数
  19. int length = in.nextInt();
  20. // 使用指定位数创建新游戏
  21. Mastermind game = new Mastermind(length);
  22. // 嘘!为了测试我们的程序是否有效,密码是:
  23. System.out.print("嘘!只是为了测试,密码是:");
  24. game.printCode();
  25. // 准备一个数组来存储用户的猜测
  26. int[] guess = new int[length];
  27. // 允许用户尝试4次来破解密码
  28. for (int j = 0; j < 4; j++) {
  29. // 告诉用户进行一次猜测
  30. System.out.println("进行一次猜测");
  31. // 循环与密码位数相同的次数
  32. for (int i = 0; i < length; i++) {
  33. // 要求用户输入他们猜测的数字
  34. System.out.print("请输入密码的下一个数字:");
  35. // 将他们的猜测存储在猜测数组中
  36. guess[i] = in.nextInt();
  37. }
  38. // 调用Mastermind类中检查猜测是否正确的方法
  39. int guessResult = game.attempt(guess);
  40. if (guessResult < 0) {
  41. System.out.println("您赢了!");
  42. return;
  43. } else {
  44. System.out.print(" ");
  45. for (int i = 0; i < guessResult; i++)
  46. System.out.print("X");
  47. System.out.print("\n\n");
  48. }
  49. }
  50. // 如果退出循环,表示用户无法破解密码,游戏结束
  51. System.out.println("~~~游戏结束~~~");
  52. }
  53. }
  54. class Mastermind {
  55. private final int[] code;
  56. // 重载构造函数
  57. public Mastermind(int length) {
  58. // 根据用户请求的位数创建数组
  59. code = new int[length];
  60. // 遍历数组
  61. for (int i = 0; i < code.length; i++) {
  62. // 向密码添加一个随机数字(在0到2之间)
  63. code[i] = (int) (Math.random() * 3 + 0);
  64. }
  65. }
  66. // 用于检查用户猜测是否正确的方法
  67. public int attempt(int[] guess) {
  68. if (Arrays.equals(guess, code)) {
  69. // 如果玩家赢了,返回-1
  70. return -1;
  71. } else {
  72. int correctDigits = 0;
  73. for (int i = 0; i < code.length; i++) {
  74. if (code[i] == guess[i]) {
  75. correctDigits++;
  76. }
  77. }
  78. return correctDigits;
  79. }
  80. }
  81. // 创建此方法,以便我们可以检查我们的游戏是否有效
  82. public void printCode() {
  83. StringBuilder answer = new StringBuilder();
  84. // 遍历密码,将数字连接到答案字符串中
  85. for (int j : code) {
  86. answer.append(j);
  87. answer.append(" ");
  88. }
  89. // 删除最后的空格
  90. String answerString = answer.substring(0, answer.length() - 1);
  91. // 打印密码
  92. System.out.println(answerString);
  93. }
  94. }
英文:

Here is a functional version of your code:

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class MastermindGame {
  4. public static void main(String[] args) {
  5. // game instructions
  6. System.out.println(&quot;Welcome to Mastermind!&quot;);
  7. System.out.println(&quot;The computer will generate a passcode with your specified number of digits.&quot;);
  8. System.out.println(&quot;Your job is to break that code!&quot;);
  9. System.out.println(&quot;After each guess, the computer will tell you how many correct digits are in the correct position.&quot;);
  10. System.out.println(&quot;It will do this by printing one X for each correct digit.&quot;);
  11. System.out.println(&quot;It does not tell you which digit is correct.&quot;);
  12. System.out.println(&quot;If you do not see any Xs print, none of your digits were correct.&quot;);
  13. System.out.println(&quot;GOOD LUCK!&quot;);
  14. // ask user to determine how many digits the code will be
  15. System.out.println(&quot;How many digits would you like the code to be?&quot;);
  16. // enable input from the user
  17. Scanner in = new Scanner(System.in);
  18. // read the integer the user types
  19. int length = in.nextInt();
  20. // create the new game with the indicated number of digits
  21. Mastermind game = new Mastermind(length);
  22. // ssshh!! cheating so we can see if this works!
  23. System.out.print(&quot;Shhh! Just so we know if our program works, the code is &quot;);
  24. game.printCode();
  25. // set up an array to hold the user&#39;s guesses
  26. int[] guess = new int[length];
  27. // allow the user 4 tries to break the code
  28. for (int j = 0; j &lt; 4; j++) {
  29. // tell the user to take a guess
  30. System.out.println(&quot;Gander a guess&quot;);
  31. // loops as many digits (length) as they indicated the code should be
  32. for (int i = 0; i &lt; length; i++) {
  33. // ask the user to input their guess for this digit
  34. System.out.print(&quot; What is the next digit for the code?\n &quot;);
  35. // store their guess in the guess array
  36. guess[i] = in.nextInt();
  37. }
  38. // call the method in the Mastermind class that checks their guess against the code
  39. int guessResult = game.attempt(guess);
  40. if (guessResult &lt; 0) {
  41. System.out.println(&quot;YOU WIN!!&quot;);
  42. return;
  43. } else {
  44. System.out.print(&quot; &quot;);
  45. for (int i = 0; i &lt; guessResult; i++)
  46. System.out.print(&quot;X&quot;);
  47. System.out.print(&quot;\n\n&quot;);
  48. }
  49. }
  50. // if we get outside the loop, user wasn&#39;t able to guess, &quot;game over&quot;
  51. System.out.println(&quot;~~~GAME OVER~~~&quot;);
  52. }
  53. }
  54. class Mastermind {
  55. private final int[] code;
  56. // overloaded constructor
  57. public Mastermind(int length) {
  58. //get the length of the array to the number of digits the user requested
  59. code = new int[length];
  60. // walk through the code
  61. for (int i = 0; i &lt; code.length; i++) {
  62. //add a random digit (0-2 in this case) to the code
  63. code[i] = (int) (Math.random() * 3 + 0);
  64. }
  65. }
  66. // method to check the user&#39;s guess against the code
  67. public int attempt(int[] guess) {
  68. if (Arrays.equals(guess, code)) {
  69. // Return -1 if the player win
  70. return -1;
  71. } else {
  72. int correctDigits = 0;
  73. for (int i = 0; i &lt; code.length; i++) {
  74. if (code[i] == guess[i]) {
  75. correctDigits++;
  76. }
  77. }
  78. return correctDigits;
  79. }
  80. }
  81. // created this method, so we can cheat to check that our game works
  82. public void printCode() {
  83. StringBuilder answer = new StringBuilder();
  84. // walk through code, concatenating the digit to the answer string
  85. for (int j : code) {
  86. answer.append(j);
  87. answer.append(&quot; &quot;);
  88. }
  89. // Remove the last space
  90. String answerString = answer.substring(0, answer.length() - 1);
  91. // print the code
  92. System.out.println(answerString);
  93. }
  94. }

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:

确定