我需要找到一种方法来计算while循环运行的次数

问题描述 投票:-1回答:1

我做到了,但是我唯一的问题是计算玩家玩游戏的次数(计数)。如何计算用户玩的次数?

这是一个剪刀石头布游戏,我要做的是创建一个循环,直到用户停止循环,然后在循环结束时计算赢,输和平的次数。] >

我主要关心的是如何计算(剧本)以及应该在哪里循环?

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random rand = new Random();
        String humanResponse = "";
        int countHumanwin = 0;
        int countHuman1 = 0;
        int countComputerwin = 0;
        int ties = 0;
        int countplays = 0;

        while (!humanResponse.equalsIgnoreCase("quit")) {
            String result;
            String[] computerResponse = { "", "Rock", "Paper", "Scissors" };

            int which = rand.nextInt(3) + 1;

            System.out.println("Enter your input: (rock, paper, or scissors)" + "\nEnter \"quit\" to exit");
            humanResponse = scan.next();
            if (humanResponse.equalsIgnoreCase("quit")) {
                continue;
            } else {
                System.out.println("I picked " + computerResponse[which] + " and you picked " + humanResponse);
            }

            result = "I won";
            countHumanwin = 2;

            if (humanResponse.equalsIgnoreCase(computerResponse[which])) {
                result = "it's a tie";
                countHumanwin = 0;
            } else if (humanResponse.equalsIgnoreCase("rock") && computerResponse[which].equalsIgnoreCase("scissors")) {
                result = "You won!";
                countHumanwin = 1;
            } else if (humanResponse.equalsIgnoreCase("paper") && computerResponse[which].equalsIgnoreCase("rock")) {
                result = "You won!";
                countHumanwin = 1;
            } else if (humanResponse.equalsIgnoreCase("scissors")
                    && computerResponse[which].equalsIgnoreCase("paper")) {
                result = "You won!";
                countHumanwin = 1;
            }

            System.out.println(result);
        }

        for (int i = 0; i < countplays; ++i) {

            if (countHumanwin == 1) {
                countHuman1 = countHuman1 + 1;
            } else if (countHumanwin == 0) {
                ties = ties + 1;
            } else if (countHumanwin == 2) {
                countComputerwin = countComputerwin + 1;
            }
        }

        System.out.println("Number of user wins: " + countHuman1);

        System.out.println("Number of user losses: " + countComputerwin);

        System.out.println("Number of ties: " + ties);

    }
}

我做到了,但是我唯一的问题是计算玩家玩游戏的次数(计数)。如何计算用户玩的次数?这是一张石头纸...

java loops for-loop while-loop counting
1个回答
0
投票

您已经具有while循环的逻辑来确定玩家何时赢,输或与计算机建立联系。您要做的就是增加适当的计数器,然后在用户输入“退出”时显示它。

© www.soinside.com 2019 - 2024. All rights reserved.