我试图创建一个随机数生成器程序,跟踪玩家的胜利,损失,获胜百分比和总奖金。该程序的逻辑是,玩家每节获得3次机会,计算机生成一个随机数,玩家需要猜测或者更确切地说应该匹配。
我试过用如果
如果您对此有任何意见,我们将不胜感激。
游戏类别:
import java.util.Scanner;
public class Game {
Player player;
LuckyNumberGenerator lng;
public Game() {
player = new Player();
lng = new LuckyNumberGenerator();
}
public void eventLoop() {
Scanner scanner = new Scanner(System.in);
int choice = 0;
boolean exit = false;
while (!exit) {
System.out.println("Welcome to the Guessing Game");
System.out.println("==============================");
System.out.println("(1) Set Up New Player");
System.out.println("(2) Play One Round");
System.out.println("(3) Player Win Statistics");
System.out.println("(4) Display Game Help");
System.out.println("(5) Exit Game");
System.out.println("Choose an option: ");
try {
choice = Integer.parseInt(scanner.nextLine());
if (choice < 1 || choice > 5) {
System.err.println("Error : Choose an option between 1 and 5");
choice = 0;
}
} catch (NumberFormatException e) {
System.err.println("Error : Choose an option between 1 and 5");
choice = 0;
}
switch (choice) {
case 1:
createNewPlayer(scanner);
break;
case 2:
guessNumber(scanner);
break;
case 3:
printStatistics();
break;
case 4:
printHelp();
break;
case 5:
exit = true;
}
}
scanner.close();
}
public void printHelp() {
System.out.println(" ");
}
public void printStatistics() {
try {
if (player.getName() == null || player.getName().trim().length() < 1) {
System.out.println("Player has not been set up!");
} else {
System.out.println("Player statistics are: ");
System.out.println("Name: " + player.getName());
System.out.println("Wins: " + player.getGamesWon());
System.out.println("Losses: " + player.getGamesLost());
System.out.println("Amount won so far: " + player.getTotalWinnings());
System.out.println("Winnings percentage : "
+ (((player.getGamesWon()) / (player.getGamesWon() + player.getGamesLost())) * 100));
}
} catch (ArithmeticException ae) {
System.out.println("wins and loss both are 0: divide by zero exception");
}
}
public void guessNumber(Scanner scanner) {
int compGuess = lng.generate();
int userGuess = 0;
int numAttempts = 0;
int cnum = lng.generateConsole();
do {
try {
System.out.println("Guess a number between 1-100: ");
userGuess = Integer.parseInt(scanner.nextLine());
if (userGuess < 1 || userGuess > 100) {
System.err.println("Error : your Guess must be between 1-100");
}
} catch (Exception e) {
System.err.println("Error : your Guess must be between 1-100");
}
} while (userGuess < 1 && userGuess > 100);
do {
if (userGuess > compGuess) {
System.out.println("Your Guess is: " + userGuess + "and the random number: " + compGuess);
System.out.println("Sorry, you need to go LOWER :");
} else if (userGuess < compGuess) {
System.out.println("Your Guess is: " + userGuess + "and the random number: " + compGuess);
System.out.println("Sorry, you need to go HIGHER :");
}
numAttempts++;
if (userGuess == compGuess) {
System.out.println("Lucky Number was : " + compGuess + "your guess was : " + userGuess);
System.out.println("Congratulations you won " + 10 + "$");
player.setGamesWon(1);
player.setTotalWinnings(10);
}
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) && (userGuess >= (compGuess - 5))) {
System.out.println("Lucky Number was : " + compGuess + "your FINAL guess was : " + userGuess);
System.out.println("Congratulations you won " + cnum + "$");
player.setTotalWinnings(5);
} else if (userGuess != compGuess) {
System.out.println("Lucky Number was : " + compGuess + "your guess was : " + userGuess);
System.out.println("Sorry better Luck Next time");
player.setGamesLost(1);
player.setTotalWinnings(-1);
}
} while (userGuess != compGuess && numAttempts < 3);
}
public void createNewPlayer(Scanner scanner) {
String name = null;
do {
try {
System.out.println("Enter the name of the player: ");
name = scanner.nextLine();
if (name.isEmpty()) {
System.err.println("Name cannot be empty");
}
} catch (Exception e) {}
} while (name.isEmpty());
this.player = new Player(name);
}
public static void main() {
Game game = new Game();
game.eventLoop();
}
}
while(userGuess < 1 && userGuess > 100);
do{
这包含错误。当userGuess在1到100之间时,循环应该运行,您已经排除了它。
应该是
while(userGuess >= 1 && userGuess <= 100);
do{
试着把第一个动作放在第二个动作上,同时放在最上面。
do {
try {
System.out.println("Guess a number between 1-100: ");
userGuess = Integer.parseInt(scanner.nextLine());
if (userGuess < 1 || userGuess > 100) {
System.err.println("Error : your Guess must be between 1-100");
}
} catch (Exception e) {
System.err.println("Error : your Guess must be between 1-100");
}
} while(userGuess < 1 && userGuess > 100);//incorrect
//correct condition -> while(userGuess < 1 || userGuess > 100);
数字不能同时小于1和大于100。
编辑1:在您的第二个循环中,以下两个条件
1) if (userGuess != compGuess && (userGuess <= (compGuess + 5))
&& (userGuess >= (compGuess - 5)))
2)else if (userGuess != compGuess)
仅当玩家的猜测次数超过尝试次数时,才应进行评估,因此两个if条件应写入循环之外。
此外,您还需要第一个而循环来保持用户输入在1-100之间有效,您的第二个而循环将在其中。
最终的代码看起来像这样。
do {
do {
try {
System.out.println("Guess a number between 1-100: ");
userGuess = Integer.parseInt(sc.nextLine());
if (userGuess < 1 || userGuess > 100) {
System.err
.println("Error : your Guess must be between 1-100");
}
} catch (Exception e) {
System.err
.println("Error : your Guess must be between 1-100");
}
} while (userGuess < 1 || userGuess > 100);
System.out.println(" " + userGuess);
if (userGuess > compGuess) {
System.out.println("Your Guess is: " + userGuess
+ "and the random number: " + compGuess);
System.out.println("Sorry, you need to go LOWER :");
}
if (userGuess < compGuess) {
System.out.println("Your Guess is: " + userGuess
+ "and the random number: " + compGuess);
System.out.println("Sorry, you need to go HIGHER :");
System.out.println("if 1");
}
numAttempts++;
if (userGuess == compGuess) {
System.out.println("Lucky Number was : " + compGuess
+ "your guess was : " + userGuess);
System.out.println("Congratulations you won " + 10 + "$");
// player.setGamesWon(1);
// player.setTotalWinnings(10);
}
} while (userGuess != compGuess & numAttempts < 3);
if (userGuess != compGuess && (userGuess <= (compGuess + 5))
|| (userGuess >= (compGuess - 5))) {
System.out.println("Lucky Number was : " + compGuess
+ " your FINAL guess was : " + userGuess);
// System.out.println("Congratulations you won " + cnum + "$");
// player.setTotalWinnings(5);
} else if (userGuess != compGuess) {
System.out.println("Lucky Number was : " + compGuess
+ "your guess was : " + userGuess);
System.out.println("Sorry better Luck Next time");
// player.setGamesLost(1);
// player.setTotalWinnings(-1);
}
}
我有一些YAML文件包含很多配置,但我只需要其中的一个标记(mytag)。 我需要加载: 如何使用SnakeYaml做到这一点?我没能很快找到任何有用的例子。 跟进:如果我的标签嵌套得更深,该怎么办:(boo/mytag) 谢谢
我想把一个字符串拆分成单个单词。如果字符串是: 我想存储为: 如果我使用则变成。我该怎么做呢
问题内容: 鉴于以下课程, 假设在调用ClassOne.main之后还有其他事情要做,则下面的类也将被破坏。 有没有办法忽略System.exit(1); ClassTwo的调用中对ClassOne的了解? 问题答案: 您 本身 不能忽略它,但是可以防止它通过SecurityManager终止JVM。查看此问题以获取详细的代码示例。
本文向大家介绍C程序打印所有ASCII值。,包括了C程序打印所有ASCII值。的使用技巧和注意事项,需要的朋友参考一下 问题 打印0到255个字符的美国信息交换标准代码(ASCII)值,而无需将该字符初始化为整数类型变量。只需使用格式说明符。 解决方案 在这里,我们正在编写一个程序,仅打印65到122。 如果要查看所有ASCII值,则在for循环中可以编写如下: 然后,它打印从0到255的所有AS
问题内容: 我有一个Golang程序,该程序从命令行读取字符串参数,并将其传递给fmt.Sprintf函数。假设tmp_str是命令行中的目标字符串。 在某些情况下,该程序将传递完整的字符串,例如“ Hello Friends”,而不是字符串模板。.程序会惊慌并返回: 你好朋友%!(EXTRA string = world) 因此,如何忽略fmt.Sprintf的额外字段? 问题答案: 是的,您可
我正在使用Jackson ObjectMapper序列化POJO。我在POJO中有嵌套字段。例如:序列化类 如果字符串为,则不序列化整个属性。也就是说,如果字符串为,我希望输出为: 但我得到的是作为输出。 我已经为序列化包含设置了(),但这并不能解决问题