Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用安全破解游戏的方法_Java_Methods - Fatal编程技术网

Java 使用安全破解游戏的方法

Java 使用安全破解游戏的方法,java,methods,Java,Methods,我正忙于学习Java,我的任务是制作一个安全破解游戏。我需要用类和方法来做这个游戏。但我已经说到点子上了,我不能再往前走了。下面我分享我的代码和我的问题。如果你能看一看,我将不胜感激 import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { entrance(); playGame();

我正忙于学习Java,我的任务是制作一个安全破解游戏。我需要用类和方法来做这个游戏。但我已经说到点子上了,我不能再往前走了。下面我分享我的代码和我的问题。如果你能看一看,我将不胜感激

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

尝试在游戏中使用循环

您可以通过playGame方法设置quitGame变量,也可以为用户决策创建新方法

public static void main(String [] args){

   entrance();
   do{
      playGame();

   }while(!quitGame)

}

public void playGame(){
   //Your code is here
}
如果我返回这个方法,它会一次又一次地运行。但我 也不知道我需要给出哪种返回类型

你的
playGame*(
方法在其最后一行递归调用自己
return playGame()
。我猜你这样做是为了返回任何东西。如果你考虑你的问题,你可能会得出结论,你根本不想返回任何东西(因为你不知道如何处理它)。在这种情况下,您可以像在
main
方法中那样不返回任何内容

我想问问用户,如果他们愿意 玩还是不玩,根据答案,我想运行“playGame” 方法,否则退出游戏

你必须考虑你想要什么。你想根据一个条件一次又一次地调用一个方法。为此,你可以使用循环或递归。对于exmaple,你可以稍微更改你的
main
方法,并添加一个do while循环

public static void main(String[] args) {
    entrance();
    int condition;
    do {
        playGame();
        condition = quitGame();
    } while (condition == 1);
不要忘记更改您的
quitGame
方法,因为您正在尝试递归地解决问题(删除if子句)。如果您想递归地解决问题,请忽略上述内容并查看此代码段:

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
}

非常感谢您的帮助Dominik。但是使用condition=quitGame();code,我收到一条错误消息,上面写着“required int,find void”。我发布的代码片段非常出色,它们是两种不同的解决方案。您是否也在quitGame()中更改了
return userWannaPlay
?您仍然必须返回它。(更好的方法是使用布尔值,只需检查
,而(!quitGame()
其中
quitGame
如果用户想要退出,则返回true,否则返回false)我现在返回它,错误消息消失。