使用安全饼干游戏的方法 [英] Working with methods for a safecracker game

查看:91
本文介绍了使用安全饼干游戏的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习Java,所以我的任务是制作一个安全的饼干游戏.我需要使用类和方法来做这个游戏.但是我到了某个地步,我再也走不了了.下面,我分享我的代码和问题.如果您可以看一下,我将非常感激.

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.
    }
}

推荐答案

如果我返回此方法,它将继续反复播放.但是我 还不知道我需要给哪种返回类型.

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.

您的playGame*(方法在其最后一行return playGame()中递归调用自身.我想您这样做是为了返回任何东西.如果考虑到您的问题,您可能会得出结论,您根本不希望返回任何东西(因为您不知道该怎么做).在这种情况下,您可能不会像在main方法中那样返回void.

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.

还有quitGame方法.我想问用户是否 玩与否,并根据答案我想运行"playGame" 再次使用方法或退出游戏

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

您必须考虑自己想要什么.您希望根据条件一次又一次地调用方法.为此,您可以使用循环或递归.例如,您可以稍微更改main方法并添加一个do-while循环.

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);

请不要忘记更改您的quitGame方法,因为您正在尝试递归解决问题(删除if子句).如果您想递归地执行此操作,请忽略上面的内容并查看以下代码段:

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
}

这篇关于使用安全饼干游戏的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆