简体   繁体   中英

Java scanner sometimes requires input twice

Not sure what's happening but when I'm using a scanner to read an int, it'll read it and then repeat the "question" statement. It's like the program will just not acknowledge the input. This is completely random as well and does not happen every time. Sometimes it'll only ask once and other times it'll ask at most 2 times. Can anyone help I'm somewhat new to Java and have been stuck for about 2 hours?

    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();
    
    }
}

If player picks Paper (2) and computer picks Lizard (4), then:

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

And on input = 3, your calc loop returns 5 (because NONE of the 3 options trigger; 2 isn't equal to 4, and the mod is neither higher than, nor lower than 3.

5 is clearly intended as an impossible 'uhoh', but the rest of your code doesn't check it. This is one of about 10 quite problematic code styles you've chosen. As a general rule of thumb 'return weird result in unexpected/seemingly impossible scenarios' is a very bad idea. You're never going to check those. Throw an exception. just.. throw new IllegalStateException("I really did not think we would ever get here"); is fine. Then you'd have known, instead of spending 2 hours, and then asking on SO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM