Java 石头剪刀布游戏

Java 石头剪刀布游戏,java,Java,我是编程新手,我一直在尝试创建一个简单的石头剪刀游戏。基本上,它使用while循环并询问用户是否要播放(或继续)。一旦他们不再想要,程序就必须打印出游戏总数、赢家数量、输家数量和赢家百分比。我已经完成了整个计划,除了它总是说胜利的百分比是0.0%,即使它不是。我已经使用了if语句来避免任何被零除的错误。我没有得到任何运行时或编译器错误,所以我要么遗漏了什么,要么就是有一个逻辑错误我就是找不到。我想继续使用扫描仪 import java.util.Scanner; public class R

我是编程新手,我一直在尝试创建一个简单的石头剪刀游戏。基本上,它使用while循环并询问用户是否要播放(或继续)。一旦他们不再想要,程序就必须打印出游戏总数、赢家数量、输家数量和赢家百分比。我已经完成了整个计划,除了它总是说胜利的百分比是0.0%,即使它不是。我已经使用了if语句来避免任何被零除的错误。我没有得到任何运行时或编译器错误,所以我要么遗漏了什么,要么就是有一个逻辑错误我就是找不到。我想继续使用扫描仪

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;

            } 
        }

    }

}

}

问题在于
双倍全胜=(int)(玩家/玩家展示)*100。由于
playerWins
gamesPlayed
都是整数类型(特别是
int
类型),Java正在进行“整数除法”,它返回除法的商作为结果,并忽略余数。因此,为了防止它这样做,您最好将该行更改为:

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

问题出在
double totalWins=(int)(玩家/玩家展示)*100。由于
playerWins
gamesPlayed
都是整数类型(特别是
int
类型),Java正在进行“整数除法”,它返回除法的商作为结果,并忽略余数。因此,为了防止它这样做,您最好将该行更改为:

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

错误在这里:
(int)(playerWins/gamesPlayed)*100。提示:整数除法。对于同样使用win矩阵的R-P-S枚举解决方案的示例,请查看以下错误:
(int)(playerWins/gamesPlayed)*100。提示:整数除法。例如,R-P-S的枚举解决方案也使用win矩阵,请看一看,谢谢,这解决了问题!但是我有点困惑,为什么这个解决方案与显式地将两个变量转换为double相反?显式转换为double的问题是,结果会得到一个类似
0.8
的结果,然后将其转换为一个整数(失去小数部分),给出
0
。然后将其乘以
100
,得到
0
。当这个方法给出(在这个例子中)
(800.0)/(10)
并且Java执行除法时,它返回double
80.0
,它将把它转换成一个整数,给出
80
。现在我想起来它是有意义的。非常感谢。谢谢,这就解决了问题!但是我有点困惑,为什么这个解决方案与显式地将两个变量转换为double相反?显式转换为double的问题是,结果会得到一个类似
0.8
的结果,然后将其转换为一个整数(失去小数部分),给出
0
。然后将其乘以
100
,得到
0
。当这个方法给出(在这个例子中)
(800.0)/(10)
并且Java执行除法时,它返回double
80.0
,它将把它转换成一个整数,给出
80
。现在我想起来它是有意义的。非常感谢。