2015-03-16 1 views
-1

Мой класс код: http://notepad.cc/lureascu84 Мой тестер код: http://notepad.cc/mammivo62Java: Количество угадайку

я застрял на том, как импортировать код в программе класса для использования в тестере. На моей простой программе номер догадок у меня есть:

while (guess != num){ 
    guess = scanner.nextInt(); 

    if (guess > num){ 
     System.out.println("The number you have entered is too high!"); 
     numberOfGuesses++; 
    } 

    if (guess < num){ 
     System.out.println("The number you have entered is too low!"); 
     numberOfGuesses++; 
    } 
    } 
    System.out.println("You win with only " + numberOfGuesses + " wrong attempts!"); 

При использовании аксессора и мутатор метод я застрял на том, что писать.

+0

насчет метода setGuess? Внутри вашего теста ваш приглашенный пользователь, setGuess на экземпляре obj, тогда метод checkGuess возвращает true, если угадание num. вам понадобятся переменные догадки, а также его помощник. –

+0

. Обновите свой вопрос, чтобы отразить все соответствующие детали, чтобы людям не нужно было щелкнуть мертвые ссылки. –

ответ

0

Я считаю, что вы застряли, создавая класс GuessingGame(int maxValue). Вам нужно создать новый объект, который вызовет конструктор класса, тогда вы сможете выполнять методы, содержащиеся внутри.

GuessingGame game = new GuessingGame(10); 

выше создает новый объект GuessingGame из вашего класса тестера, похожий на создание Random random = new Random(), где «MaxValue» был установлен, чтобы быть 10. Отсюда можно сделать вызовы методов.

game.guess(9); 

запустить бы выход guess(int newGuess) и предположительно, что догадка пользователя была слишком мала.

0

это, как я бы решить эту проблему:

//creates a public function 
public static void main(String[] args) { 
    //states that r will be the new random number 
    Random r = new Random(); 
    //sets 1 as the lowest possible number 
    int Low = 1; 
    //sets 10 as the highest number possible 
    int High = 10; 
    //generates the random number that is in between or is one of the biggest/lowest possible numbers 
    int Result = r.nextInt(High-Low) + Low; 

    //states the variable that will keep count of number of tries by the user. 
    int numOfTries = 0; 

    //prints out intro to the game 
    System.out.println("Hello! lets play a game!"); 
    System.out.println("Guess a number between 1 and 10"); 
    System.out.println(" "); 
    System.out.print("What is your guess: "); 
    System.out.println(" "); 

    //states the scan variable 
    Scanner scan = new Scanner(System.in); 

    //loop function that will repeat till satisfied 
    for (numOfTries=1; numOfTries<6;numOfTries++) { 
     // allows the user to enter their guess 
      int number = scan.nextInt(); 
     if (number == Result) { //this is what happens when the user guesses right within 5 tries 
      System.out.println("Correct! It took you " + numOfTries + " to guess the right number!"); 
      System.out.println("Game over, YOU WON!"); 
      break; 
     } 
     else if (number < Low || number > High) { //reminds the user of the guessing range 
      System.out.println("Remember that the guessing range is between " + Low + " though " + High); 
      System.out.println("Guess again..."); 
     } 
     else if (number < Result && numOfTries < 5) { //tells the user if the guess it too small 
      System.out.println("Your guess is too small. Guess again."); 

     } 
     else if (number > Result && numOfTries < 5) { //tells the user is the guess is too large 
      System.out.println("Your guess is too big. Guess again."); 

     } 
     else if (number > Result && numOfTries == 5) { //tells user game over if didn't guess right after 5 tries 
      System.out.println("Your guess is too big. GAME OVER, LOSER!"); 
      System.out.println("You were not able to guess the right number in 5 tries."); 
      System.out.println("The correct number is " + Result); 
     } 
     else if (number < Result && numOfTries == 5) { //tells the user game over if didn't guess right after 5 tries 
      System.out.println("Your guess is too small. GAME OVER, LOSER!"); 
      System.out.println("You were not able to guess the right number in 5 tries."); 
      System.out.println("The correct number was " + Result); 
     } 


} 

} 
Смежные вопросы