简体   繁体   English

剪刀石头布游戏

[英]Rock Paper Scissors game

I'm new to programming and I've been trying to create a simple Rock Paper Scissors game. 我是编程的新手,我一直在尝试创建一个简单的剪刀石头布游戏。 Basically, it uses a while loop and asks the user whether they want to play (or continue). 基本上,它使用while循环,并询问用户是否要播放(或继续)。 Once they no longer want to, the program has to print out the total number of games, number of wins, number of losses and percentage of wins. 一旦他们不再想要,该程序就必须打印出游戏总数,获胜次数,失败次数和获胜百分比。 I've got the entire program to work, except it always says the percentage of wins is 0.0%, even when it's not. 我已经完成了整个程序,但是它总是说获胜的百分比是0.0%,即使不是。 I already used an if statement to avoid any divide by zero error. 我已经使用了if语句来避免任何被零除的错误。 I'm not getting any runtime or compiler errors, so I'm either missing something or there's a logic error I just cannot find. 我没有遇到任何运行时或编译器错误,所以我丢失了某些东西,或者是我找不到的逻辑错误。 I would like to continue using the Scanner. 我想继续使用扫描仪。

import java.util.Scanner; 

public class RockPaperScissors {

/*
 * Program allows user to play Rock, Paper and Scissors as many times as desired by entering Y until they enter N.
 * Program will print amount of games played, amount lost, amount won and percentage won. 
 * User must enter "Y", "N", "Rock", "Paper" or "Scissors" with correct capitalization and spelling. 
 */ 

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int playerWins = 0;
    int compWins = 0;
    int gamesPlayed = 0;

    while (true) {
        System.out.println("Do you want to play Rock Paper Scissors (Y/N): ");
        String play = input.nextLine();

        // user terminates game and program prints number of wins, losses and percentage of wins.
        if (play.equals("N")) {

            System.out.println("You played a total of " + gamesPlayed + " matches against the computer");
            System.out.println("The computer won " + compWins + " matches");
            System.out.println("You won " + playerWins + " matches");

            // 0% wins when no games are played.
            if (gamesPlayed == 0) { 
                System.out.println("You won 0% of the time!");
                break;

            } else if (gamesPlayed > 0) {
                double totalWins = (int)(playerWins / gamesPlayed) * 100;
                System.out.println("You won " + totalWins + "% of the time!");
                break;
            }

        } else if ((!play.equals("N")) && (!play.equals("Y"))) {
            System.out.println("Invalid entry");
        } else {

            System.out.println("Welcome to Rock, Paper and Scissors!");
            System.out.print("Select \"Paper\", \"Rock\" or \"Scissors\": ");
            String decision = input.nextLine();
            System.out.println("Your selection: " + decision);

            // random number generator producing integer values between 1 to 3 for computer's choices.
            // 1 is for Rock, 2 is for Paper and 3 is for Scissors.
            int num = (int)(Math.random() *  (3-0) + 1);

            switch (num) {

                // Computer picks Rock
                case 1:
                    if (decision.equals("Rock")) {
                    System.out.println("Tie, you and the computer selected rock");
                    gamesPlayed++;
                } else if (decision.equals("Paper")) {
                    System.out.println("You win, paper beats rock!");
                    gamesPlayed++;
                    playerWins++;
                } else if (decision.equals("Scissors")) {
                    System.out.println("Computer wins, rock beats scissors!");
                    gamesPlayed++;
                    compWins++;
                } else {
                    System.out.println(decision + " is not a valid input");
                }
                break;
                case 2:
                    // computer picks Paper
                    if (decision.equals("Rock")) {
                    System.out.println("Computer wins, rock beats paper!");
                    gamesPlayed++;
                    compWins++;
                } else if (decision.equals("Paper")) {
                    System.out.println("Tie, you and the computer selected paper");
                    gamesPlayed++;
                } else if (decision.equals("Scissors")) {
                    System.out.println("You win, scissors beats paper");
                    gamesPlayed++;
                    playerWins++;
                } else {
                    System.out.println(decision + " is not a valid input");
                }
                break;
                case 3:
                    // computer picks Scissors
                    if (decision.equals("Rock")) {
                    System.out.println("You win, rock beats scissors");
                    gamesPlayed++;
                    playerWins++;
                } else if (decision.equals("Paper")) {
                    System.out.println("Computer wins, scissors beats paper");
                    gamesPlayed++;
                    compWins++;
                } else if (decision.equals("Scissors")) {
                    System.out.println("Tie, you and the computer selected scissors");
                    gamesPlayed++;
                } else {
                    System.out.println(decision + " is not a valid input");
                }
                break;

            } 
        }

    }

}

} }

The problem is at double totalWins = (int)(playerWins / gamesPlayed) * 100; 问题出在double totalWins = (int)(playerWins / gamesPlayed) * 100; . Since playerWins and gamesPlayed are both integral type (specifically int type), Java is doing 'Integer Division', which returns the quotient of the division as result and ignore the remainder. 由于playerWinsgamesPlayed均为整数类型(具体地int型),爪哇是做“整数除法”,它返回除法作为结果的商,而忽略其余的。 So to prevent it from doing this you are best changing that line to: 因此,为防止它执行此操作,您最好将该行更改为:

double totalWins = (playerWins * 100.0) / gamesPlayed;
//                 /------------------\
// This converts the `playerWins` to a `double` and does the division as you expect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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