[英]Program ignoring “If Else” and printing everything
我正在嘗試創建一個隨機數生成器程序,該程序可跟蹤玩家的獲勝,失敗,獲勝百分比和總獎金。 該程序的邏輯是,玩家每次會話獲得3次機會,並且計算機生成一個隨機數,玩家需要猜測或應該匹配該隨機數。
我嘗試使用if&else語句來告訴用戶他是否需要猜測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();
}
}
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條件寫入循環之外。
另外,您還需要第一個while循環以使用戶輸入在1到100之間有效,而第二個while循環將在其中。
最終代碼將如下所示。
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);
}
}
嘗試將第一個do-while放在第二個do-while的頂部。
while(userGuess < 1 && userGuess > 100);
do{
這包含錯誤。 當userGuess在1到100之間時,循環應該運行,您已經排除了它。
它應該是
while(userGuess >= 1 && userGuess <= 100);
do{
AND條件需要更改為OR條件。 因為您只想在用戶輸入的數字小於1或大於100時循環。
while(userGuess < 1 && userGuess > 100); ===> while(userGuess < 1 || userGuess > 100);
同樣,另一個AND需要更改為OR 。
// Issue with logic
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) &&
(userGuess >= (compGuess - 5))) {
// Corrected Code
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) ||
(userGuess >= (compGuess - 5))) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.