java - 使用安全破解游戏的方法
问题描述
我只是忙于学习 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*(
方法在其最后一行以递归方式调用自身return playGame()
。我猜你这样做是为了返回任何东西。如果您考虑您的问题,您可能会得出一个结论,即您根本不想返回任何东西(因为您不知道如何处理它)。void
在这种情况下,您可能不会像在您的main
方法中那样返回任何内容。
还有 quitGame 方法。我想问用户他们是否想玩,根据回答我想再次运行“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
}
推荐阅读
- embedded - TI CC2652RB 的嵌入式编码。如何从uart接收一串字符并将它们保存在sd卡中?
- c++ - 有没有其他方法可以制作帕斯卡三角形?
- django - 从客户端从 s3 存储桶下载图像文件
- python - 我的“蛇”在尝试以重复方式移动它时卡在 pygame 中?
- c# - 如何在 ASP.NET MVC 中使用 EntityFramework6.Npgsql 从 Postgres 数据库中获取数据?
- android - 当我想在android studio中制作movign背景时,为什么我的应用程序崩溃了?
- c# - 尝试单击特定 x 和 y 坐标时出现“移动目标越界”异常
- c++ - list::insert 是否会破坏 C++ 中的迭代器?
- google-chrome - 打开本地 HTML 文件:Chrome 将空间视为新标签?
- html - Flex Box 中的 SVG 在 Chrome 中消失