【问题标题】:Covert int to int array将 int 转换为 int 数组
【发布时间】:2017-10-06 03:49:45
【问题描述】:

所以我对编程很陌生。我正在介绍 java 类,我正在尝试提交我的项目,但是,我收到错误“int 无法转换为 int []”。该程序可以正常编译并且可以正常工作,但是当它提交给我的网络猫时。它不能引用它。

import java.util.*;
/**
 * Guess the 3 digit code and this program will tell you how many
 * digits you have right and once you guess the correct code,
 * it'll tell you how many guesses it took you.
 * Press 0 to exit the program.
 *
 * @author (philtsoi)
 * @version (10/05/2017)
 */
public class CodeCracker {
    /**
     *  calls the play method
     *  
     */
    public static void main(String[] args) {
        play();
    }
    /**
     * starts the game
     */
    public static void play() {
        System.out.println("Guess my 3-digit code?");
        Scanner in = new Scanner(System.in);

        Random random = new Random();
        int correctd = random.nextInt(900) + 100; // random 3-digit code

        int[] code = new int[3]; // array that holds 3 integers
        int extract = 0; // extract is the one digit of guess
        int input = 0; // input is the digits the player types in
        int counter = 0; // counter is the number of guesses
        int correct = counter; // correct is the digits correct 

        extract = correctd / 100;
        code[0] = extract; // first digit
        correctd = correctd - extract * 100;
        extract = correctd / 10;
        code[1] = extract; // second digit
        correctd = correctd - extract * 10;
        code[2] = correctd; // third digit

        while (true) {
            System.out.println("Your guess? ");
            input = in .nextInt();
            counter++;
            if (input == 0) {
                System.out.println("Ok.Maybe another time.");
                break;
            } else {
                correct = checkGuess(code, input);
                System.out.println(input + " - " + correct + " digits correct");
                if (correct == 3) {
                    System.out.println("You got it in " + counter + " times");
                    break;
                }
            }
        }
    }

    /**
     * This method checkGuess goes through the code and calculates each 
     * digit and returns the number of correct ones
     * 
     * @param code[] the array that the number being guesses is stored in
     * @param guess the integer of the next guessed digit
     * @return number of correct digits
     */
    public static int checkGuess(int code[], int guess) {
        int count = 0; // count is the number of digits correct
        int extract = guess / 100; // extract is the one digit of guess
        if (code[0] == extract) {
            count++;
            guess -= extract * 100;
            extract = guess / 10;
        }
        if (code[1] == extract) {
            count++;
            guess -= extract * 10;
        }
        if (code[2] == guess) {
            count++;
        }
        return count;
    }

}

我知道错误的问题是 checkGuess 方法。任何帮助将不胜感激。

这些是我得到的错误:

错误

【问题讨论】:

  • 你的错误发生在哪里
  • 哪一行导致错误?你希望这条线做什么?
  • 测试以单个三位整数的形式传入代码;您必须将code[0] = extract 等移动到函数内。
  • 测试用例期望checkGuess 方法具有以下签名:int checkGuess(int, int)。您将第一个参数指定为 int 数组。您需要使其符合测试用例。

标签: java arrays loops int


【解决方案1】:

此方法checkGuess(int code[], int guess) 需要一个数组,后跟一个 int 作为参数,您不能调用它传递 2 个 int..

测试代码的类是失败的,您将变量代码定义为int[]

【讨论】:

    【解决方案2】:

    我认为你不需要使用数组。所以,把不用的东西注释掉,直接用数字,更正

    //      int[] code = new int[3]; // array that holds 3 integers
    //      int extract = 0; // extract is the one digit of guess
            int input = 0; // input is the digits the player types in
            int counter = 0; // counter is the number of guesses
            int correct = counter; // correct is the digits correct
    
    //      extract = correctd / 100;
    //      code[0] = extract; // first digit
    //      correctd = correctd - extract * 100;
    //      extract = correctd / 10;
    //      code[1] = extract; // second digit
    //      correctd = correctd - extract * 10;
    //      code[2] = correctd; // third digit
    

    如果你只使用数字,而不是数组,应该这样做。

    public static int checkGuess(int code, int guess) {
        int count = 0; // count is the number of digits correct
        while(code != 0 && guess != 0){
            if(code % 10 == guess % 10){
                count++;
            }
            code /= 10;
            guess /= 10;
        }
        return count;
    }
    

    另外,别忘了用更正的方法调用方法,而不是数组。

    ...
            } else {
                correct = checkGuess(correctd, input);
                System.out.println(input + " - " + correct + " digits correct");
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 2012-09-28
      • 1970-01-01
      相关资源
      最近更新 更多