2017-10-06 16 views
0

私はかなりプログラミングの新しいです。私はJavaクラスの紹介をしていますが、プロジェクトを提出しようとしていますが、「intをint []に変換できません」というエラーが表示されています。プログラムはうまくコンパイルされ、それは私のweb-catに提出するときに機能します。それを参照することはできません。int 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メソッドです。どんな助けもありがとう。

これらは私が Errors

+0

エラーが発生しました – Kasnady

+0

どの行でエラーが発せられましたか?あなたはその行が何をすると思いますか? –

+0

テストでは、コード内で3桁の単一の整数として渡されます。関数内で 'code [0] = extract'などを動かさなければなりません。 –

答えて

1

この方法checkGuess(int code[], int guess)をパラメータとしてint型に続く配列を期待して、あなただけの2つのint型を渡し、それを呼び出すことはできません取得していますエラーです。..

は、クラスのテストコードが失敗した場合は、変数コードをint[]

0

と定義しましたが、配列を使用する必要はありません。だから、あなたが使用していないものをコメントアウトして、数字だけを使用してください。correctd

//  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"); 
... 
関連する問題