Java扫描器有时需要输入两次

时间:2020-08-18 01:05:43

标签: java java.util.scanner

不知道发生了什么,但是当我使用扫描仪读取一个int时,它将读取它,然后重复“问题”语句。就像程序将不会确认输入一样。这也是完全随机的,并非每次都会发生。有时,它只会问一次,其他时候最多会问两次。有人可以帮我刚接触Java大约2个小时吗?

    int gameCalc() {
        System.out.print("0:Rock, 1:Spock, 2:Paper, 3:Lizard, 4:Scissors: ");     

        playerNumber = scan.nextInt();
        computerNumber = rand.nextInt(5); 
        
        mod = playerNumber - computerNumber;
        
        playerString = resultCalc(playerNumber);
        computerString = resultCalc(computerNumber);


        if (playerNumber == computerNumber) {
            System.out.println("\nTie!");
            System.out.println(playerString + resultString + computerString);
            return 0;
        }
        else if (Math.floorMod(mod, 5) < 3){
            System.out.println("\nPlayer Wins This Round!");
            System.out.println(playerString + resultString + computerString);
            return 1;
        }    
        else if (Math.floorMod(mod, 5) > 3) {
            System.out.println("\nComputer Wins This Round!");
            System.out.println(playerString + resultString + computerString);
            return 2;
        }
        else{return 5;}
    } 

    String resultCalc(int i) {

        switch(i) {
            case 0:
                playerChoice[0] += 1;
                return "Rock";
            case 1:
                return "Spock";
            case 2:
                return "Paper";
            case 3:
                return "Lizard";
            case 4:
                return "Scissors";
            default:
                return "Invalid Input";
            }
    }

    void gameLoop() {
        int pWins, cWins;
        int tpWins, tcWins, tTies;
        int n, temp;
        String cont;
        boolean val;
        val = true;

        pWins = cWins = 0;
        tpWins = tcWins = tTies = 0;

        
        while (true) {

        System.out.print("Best Of One Press: 1. Best Of Three Press: 2. Best Of Five Press: 3. View Stats Press: 4: ");
        
        n = scan.nextInt();
            switch(n) {
                case 1:
                    pWins = cWins = 0;
                    while (val) {
                    temp = this.gameCalc();
                    
                        if (temp == 0) {tTies += 1;}
                        if (temp == 1) {pWins += 1;}
                        if (temp == 2) {cWins += 1;}

                        if (cWins == 1) {
                            System.out.println("\nComputer Wins The Match!");
                            val = false;
                        }
                        if (pWins == 1) {
                            System.out.println("\nPlayer Wins The Match!");
                            val = false;
                        }
                    }
                    break;

                case 2:
                    pWins = cWins = 0;

                    while (val) {
                        temp = this.gameCalc();
                    
                        if (temp == 0) {tTies += 1;}
                        if (temp == 1) {pWins += 1;}
                        if (temp == 2) {cWins += 1;}
                    
                        if (cWins == 2) {
                            System.out.println("\nComputer Wins The Match " + cWins + " to " + pWins);
                            val = false;
                        }
                        if (pWins == 2) {
                            System.out.println("\nPlayer Wins The Match " + pWins + " to " + cWins);
                            val = false;
                        }
                    }
                    break;
                case 3:
                    pWins = cWins = 0;

                    while (val) {
                        temp = this.gameCalc();
                        
                        if (temp == 0) {tTies += 1;}
                        if (temp == 1) {pWins += 1;}
                        if (temp == 2) {cWins += 1;}

                        if (cWins == 3) {
                            System.out.println("\nComputer Wins The Match " + cWins + " to " + pWins);
                            val = false;
                        }
                        else if (pWins == 3) {
                            System.out.println("\nPlayer Wins The Match " + pWins + " to " + cWins);
                            val = false;
                        }
                    }
                    break;
                case 4:
                    System.out.println("Stats: \nTotal Player Match Wins: " + tpWins);
                    System.out.println("Total Computer Match Wins: " + tcWins);
                    System.out.println("Total Round Ties: " + tTies);
                    for (int element: this.playerChoice) {
                        System.out.println(element);
                    }       
                    continue;

                default:
                    System.out.println("error");
            }    

            System.out.println("Enter N To Quit Or Y To Play Again: ");
            cont = scan.next();
            
            if (cont.equals("N") | cont.equals("n")) {
                scan.close();
                break;
            }
            else {val = true; continue;}    
        }


        scan.close();

    }
}
       

public class JacobCardosoRPSLZ {
    public static void main(String[] args) {
        Calculations myCalc = new Calculations();
        
        myCalc.gameLoop();
    
    }
}

1 个答案:

答案 0 :(得分:0)

如果玩家选择Paper(2),而计算机选择Lizard(4),则:

mod = -2
Math.floorMod(-2, 5) == 3

在输入= 3时,您的calc循环返回5(因为3个选项触发器中没有一个; 2不等于4,并且mod既不大于也不小于3。

5显然是作为不可能的'uhoh'来使用的,但是您的其余代码不会对其进行检查。这是您选择的大约10种有问题的代码样式之一。作为一般经验法则,“在不可预期的/看似不可能的情况下返回怪异结果”是一个非常糟糕的主意。您永远不会检查那些。引发异常。公正。throw new IllegalStateException("I really did not think we would ever get here");很好。然后,您会知道,而不是花2个小时,然后询问SO。