所以我很新的編程。我正在介紹java類,我正在嘗試提交我的項目,但是,我收到錯誤「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
你的錯誤發生在哪裏 – Kasnady
哪一行會導致錯誤?你期望那條線做什麼? –
測試作爲單個三位整數傳入代碼中;你必須將'code [0] = extract'等移動到函數中。 –