Working with methods for a safecracker game

别等时光非礼了梦想. 提交于 2019-12-11 18:17:33

问题


I am just busy with learning Java and my task is making a safecracker game. I need to do this game with classes and methods. But I came to until a point and I can't go further. Below I share my code and my questions. If you could have a look I will be so appreciate.

import java.util.Random;
import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        entrance();
        playGame();
        quitGame();
    }

     private static void entrance() {
        System.out.println("Welcome to the SafeCracker!\nI need your help to open the safe box." +
                "\nThe code is with 3 digits and we need to find it out as quick as possible.\nLet's write your guess!");
    }

     private static int playGame() {
        int[] safeCode = {takeRandomSafeCode(), takeRandomSafeCode(), takeRandomSafeCode()};
        int guess = takeGuess();

        //Below I need to use a for each loop but I don't get the logic of it. I stuck here. I need to check every numbers one by one but how?  

        for (int safeDigit : safeCode) {
            if (safeDigit == guess) {
                System.out.println("Your number is correct");

            }
        }
        return playGame(); // with this return type I also have a problem. 
If I return this method, it keeps going to play again and again.
But I don't know also which return type I need to give.
    }

    private static int takeGuess() {
        Scanner keyboard = new Scanner(System.in);
        int userGuess = keyboard.nextInt();
        return userGuess;
    }

    private static int takeRandomSafeCode() {
        Random random = new Random();
        int result = random.nextInt(10);
        return result;
    }

    private static int quitGame() {
        System.out.println("Do you want to play again?\nPress 1 for play again\nPress 2 for quit the game!");
        Scanner key = new Scanner(System.in);
        int userWannaPlay = key.nextInt();

        if(userWannaPlay == 1) {
            System.out.println(playGame());
        } else if (userWannaPlay == 2) {
            System.out.println(quitGame());
        } else {
            System.out.println("You entered an invalid number. If you want to play again or quit, you need to click 1 or 2!");
        }
    return userWannaPlay; //And also quitGame method. I want to ask the users that if they want to play or not and according to answer I would like to run "playGame" method again or quit game.
    }
}

回答1:


Try to use a loop for your game.

You can set quitGame variable from playGame method or you can create a new method for user decision.

public static void main(String [] args){

   entrance();
   do{
      playGame();

   }while(!quitGame)

}

public void playGame(){
   //Your code is here
}



回答2:


If I return this method, it keeps going to play again and again. But I don't know also which return type I need to give.

Your playGame*( method calls itself recursively in its last line return playGame(). I guess you did that to return anything at all. If you think about your problem you may come to the conclusion that you don't want to return anything at all (as you do not know what to do with it). In this case you may return nothing aka void as you did in your main method.

And also quitGame method. I want to ask the users that if they want to play or not and according to answer I would like to run "playGame" method again or quit game

You have to think about what you want. You want to call a method again and again depending on a condition. For that you can either use a loop or recursion. For exmaple you could change you main method slightly and add a do-while-loop.

public static void main(String[] args) {
    entrance();
    int condition;
    do {
        playGame();
        condition = quitGame();
    } while (condition == 1);

Don't forget to change you quitGame method because there you are trying to solve your problem recursively (remove the if clause). If you want to do it recursively though ignore the above and look at this snippet:

private static int quitGame() {
    System.out.println("Do you want to play again?\nPress 1 for play again\nPress 2 for quit the game!");
    Scanner key = new Scanner(System.in);
    int userWannaPlay = key.nextInt();

    if(userWannaPlay == 1) {
        playGame(); // you dont need a println here
    } else if (userWannaPlay == 2) {
       // you dont need to anything here
       System.out.println("Quitting...");
    } else {
        System.out.println("You entered an invalid number. If you want to play again or quit, you need to click 1 or 2!");
       // call quitGame again to ask for the choice again
       quitGame();
    }
return userWannaPlay; // if you do it like this, this return is also unnecessary and you could use a void method without returning anything
}


来源:https://stackoverflow.com/questions/50149823/working-with-methods-for-a-safecracker-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!