2017-10-06 8 views
0

그래서 나는 프로그래밍에 새로운 있습니다. 자바 클래스에 대한 소개를하고 있는데 프로젝트를 제출하려고합니다. "int를 int []로 변환 할 수 없습니다."라는 오류가 나타납니다. 프로그램을 잘 컴파일하고 작동하지만 내 웹 고양이에 제출할 때 작동합니다. 그것을 참조 할 수 없습니다.배열 int 배열에 Covert

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

으로 정의했습니다. 배열을 사용할 필요가 없다고 생각합니다. 따라서 사용하지 않는 것을 주석으로 처리하고 숫자를 사용하십시오. 수정!

//  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; 
} 

또한 배열이 아니라 correctd와 함께 메서드를 호출하는 것을 잊지 마십시오.

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