我正忙于学习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.
如果我返回此方法,它会一次又一次地继续播放。 但我也不知道我需要给哪种返回类型。 }
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.
}
答案 0 :(得分:0)
尝试为游戏使用循环。
您可以从playGame方法设置quitGame变量,也可以为用户决策创建新方法。
public static void main(String [] args){
entrance();
do{
playGame();
}while(!quitGame)
}
public void playGame(){
//Your code is here
}
答案 1 :(得分:0)
如果我返回此方法,它会一次又一次地继续播放。但是我 我也不知道我需要给哪种返回类型。
您的playGame*(
方法在其最后一行return playGame()
中递归调用自身。我想你这样做可以归还任何东西。如果您考虑到您的问题,您可能会得出结论,您根本不想归还任何东西(因为您不知道如何处理它)。在这种情况下,您可能不会像在void
方法中那样返回任何名为main
的内容。
还有quitGame方法。我想问用户是否需要 是否打球,根据回答,我想跑#34; playGame" 方法再次或退出游戏
你必须考虑你想要的东西。您希望根据条件一次又一次地调用方法。为此,您可以使用循环或递归。例如,您可以稍微更改main
方法并添加一个do-while-loop。
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
}