Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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_Bluej - Fatal编程技术网

Java “程序忽略”;如有其他",;打印所有的东西

Java “程序忽略”;如有其他",;打印所有的东西,java,bluej,Java,Bluej,我试图创建一个随机数生成器程序,跟踪玩家的赢、输、赢百分比和总赢。该程序的逻辑是,一名玩家每节课有3次机会,计算机生成一个随机数,玩家需要猜测或者应该匹配 我尝试使用if&else语句告诉用户,在允许的3次猜测中,他是需要猜测一个更高的数字还是一个更低的数字。现在的情况是,它完全忽略了条件,一次打印出所有三次机会,并结束了比赛 如果您对此有任何意见,我们将不胜感激 游戏类别: import java.util.Scanner; public class Game { Player p

我试图创建一个随机数生成器程序,跟踪玩家的赢、输、赢百分比和总赢。该程序的逻辑是,一名玩家每节课有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();
    }

}
import java.util.Scanner;
公开课游戏{
玩家;
幸运号发电机液化天然气;
公共游戏(){
player=新玩家();
lng=新的LuckyNumberGenerator();
}
公共void eventLoop(){
扫描仪=新的扫描仪(System.in);
int-choice=0;
布尔退出=假;
当(!退出){
System.out.println(“欢迎来到猜谜游戏”);
System.out.println(“====================================================”);
System.out.println(“(1)设置新播放器”);
System.out.println(“(2)播放一轮”);
System.out.println(“(3)玩家获胜统计”);
System.out.println(“(4)显示游戏帮助”);
System.out.println(“(5)退出游戏”);
System.out.println(“选择一个选项:”);
试一试{
choice=Integer.parseInt(scanner.nextLine());
如果(选项<1 | |选项>5){
System.err.println(“错误:在1和5之间选择一个选项”);
选择=0;
}
}捕获(数字格式){
System.err.println(“错误:在1和5之间选择一个选项”);
选择=0;
}
开关(选择){
案例1:
createNewPlayer(扫描仪);
打破
案例2:
数字(扫描器);
打破
案例3:
printStatistics();
打破
案例4:
打印帮助();
打破
案例5:
退出=真;
}
}
scanner.close();
}
public-void-printHelp(){
System.out.println(“”);
}
公共统计数据(){
试一试{
如果(player.getName()==null | | player.getName().trim().length()<1){
System.out.println(“尚未设置播放器!”);
}否则{
System.out.println(“玩家统计为:”);
System.out.println(“Name:+player.getName());
System.out.println(“Wins:+player.getGamesWon());
System.out.println(“loss:+player.getGamesLost());
System.out.println(“到目前为止赢得的金额:+player.getTotalWinnings());
System.out.println(“赢款百分比:”
+(((player.getGamesWon())/(player.getGamesWon()+player.getGamesLost())*100));
}
}捕获(算术异常ae){
System.out.println(“赢和输都是0:除零例外”);
}
}
公共空白猜测编号(扫描仪){
int compGuess=lng.generate();
int userGuess=0;
int numatempts=0;
int cnum=lng.generateConsole();
做{
试一试{
System.out.println(“猜一个介于1-100之间的数字:”);
userGuess=Integer.parseInt(scanner.nextLine());
如果(用户猜测<1 | |用户猜测>100){
System.err.println(“错误:您的猜测必须在1-100之间”);
}
}捕获(例外e){
System.err.println(“错误:您的猜测必须在1-100之间”);
}
}而(userGuess<1&&userGuess>100);
做{
如果(userGuess>compGuess){
System.out.println(“您的猜测是:“+userGuess+”,随机数是:+compGuess”);
System.out.println(“对不起,您需要降低:”);
}else if(userGuesswhile(userGuess < 1 && userGuess > 100);
        do{
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) if (userGuess != compGuess && (userGuess <= (compGuess + 5))
            && (userGuess >= (compGuess - 5))) 

2)else if (userGuess != compGuess)
    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);

    }

}
while(userGuess < 1 && userGuess > 100);  ===> while(userGuess < 1 || userGuess > 100);
// Issue with logic
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) &&
               (userGuess >= (compGuess - 5))) {

// Corrected Code
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) ||
               (userGuess >= (compGuess - 5))) {