在while循环中保持分数的问题(Java)

时间:2019-03-07 00:16:39

标签: java loops for-loop while-loop

我正在介绍Java类,对于我的一项作业,我必须使用循环(一段时间)来跟踪我和计算机之间的分数。

这是我教授讲的确切词:

编写一个执行此操作的程序:您(作为程序员)是经销商。 为自己选择一个随机数(0-100之间)。要求用户输入一个随机数(0到100之间),接近21的那个人将赢得比赛。

(第2部分)-循环(保持计数器)运行相同的程序并将其继续运行,以便它继续播放(打手并说出谁获胜),直到用户输入21为止,此时您将打印一些统计数据并说再见。例如,您的再见可能如下所示:

回合数:5 经销商赢了:3 玩家获胜:2

您2分代表5分。

现在,我已经编写了代码并在其中运行了几个小时,并且无法使其循环工作。我已经尝试了片刻,做了片刻,并且为。我已经在互联网上到处寻找类似的示例,但是无论如何都无法在程序中进行循环。如果有人有任何建议,我将不胜感激。

我的代码:

    import java.util.*;

class asd {


    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome Valued player, take your guess!");


        int min = 0;
        int max = 100;
        int input;
        int c = 21;
        int count = 0;
        int userscore = 0;
        int dealerscore = 0;
        int gamesplayed = 0;


        Random rand = new Random();


        int r = rand.nextInt(max - min) + min;
        input = sc.nextInt();
        System.out.println("computer's number:" + r);


        if (Math.abs(input - c) <= Math.abs(r - c)) {
            System.out.println("the winner is the user!" + input);
            dealerscore++;
            gamesplayed++;


        } else {
            System.out.println("the winner is the computer!" + r);
            userscore++;
            gamesplayed++;
        }
        if (input == c) {
            System.out.println("thank you for playing. you won.");

        }

        if (r == c) {
            System.out.println("Thank you for playing:" + userscore);
            System.out.println(userscore);


        }

        if (input == 0) {
            System.out.println("Number of hands played:" + gamesplayed);
            System.out.println("Dealer won:" + dealerscore);
            System.out.println("User won:" + userscore);
        }

        while (input != c && r != c)
            gamesplayed++;


    }


    // TODO code application logic here
}

一切正常,但是我无法在这里的任何地方运行循环。

3 个答案:

答案 0 :(得分:1)

您需要一个包含游戏逻辑的while循环。该条件应仅检查input != c是否正确。

然后在循环内,继续询问用户输入。另外,在添加分数时,您将userscoredealerscore混合了。

然后最后,一旦您退出循环,就可以打印分数/统计信息。

请阅读以下评论:

import java.util.*;

public class MyClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome Valued player, take your guess!: ");

        int min = 0;
        int max = 100;
        int input;
        int c = 21;
        int count = 0;
        int userscore = 0;
        int dealerscore = 0;
        int gamesplayed = 0;

        Random rand = new Random();
        int r = rand.nextInt(max - min) + min;
        input = sc.nextInt();

        /*
        This loop runs the game until the user enters 21
        */
        while (input != c) {
            System.out.println("Computer's number:" + r);

            if (Math.abs(input - c) <= Math.abs(r - c)) {
                System.out.println("The winner is the user! " + input);
                userscore++; //You mixed up userscore and dealerscore
            } else {
                System.out.println("The winner is the computer! " + r);
                dealerscore++; //You mixed up userscore and dealerscore
            }

            /*
            User needs to keep entering guesses
            */
            System.out.println();
            System.out.println("Enter another guess: ");
            r = rand.nextInt(max - min) + min;
            input = sc.nextInt();
        }

        /*
        You don't need any conditions since the games have already ended
        But it should be outside and after the loop
        */
        System.out.println("Number of hands played:" + gamesplayed);
        System.out.println("Dealer won:" + dealerscore);
        System.out.println("User won:" + userscore);
    }
}

答案 1 :(得分:0)

使用

更改循环
var response = await axios.get("https://graph.microsoft.com/v1.0/me/photo/$value", {
    responseType: 'arraybuffer'
});

您将看到它正在工作。我会更好地格式化代码,以便更轻松地调试它,并始终使用方括号。

答案 2 :(得分:0)

尝试一下。只需参考代码以获得解释。

import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome Valued player, take your guess!");
        System.out.println("");

        int min = 0;
        int max = 100;
        int input;
        int c = 21;
        int userscore = 0;
        int dealerscore = 0;
        int gamesplayed = 1;

        Random rand = new Random();

        while (true) { // so that the game will keep playing
            System.out.println("----------------- ROUND " + gamesplayed + " -----------------");
            int r = rand.nextInt(max - min) + min; // computer's choice

            while (true) { // so that it will keep asking the user in case the user enters an invalid input
                try {
                    System.out.print("Enter a random number! :");
                    input = Integer.parseInt(sc.nextLine());
                    break;
                } catch (Exception e) {
                    System.out.println("Invalid input!");
                }
            }

            System.out.println("The computer's random number is " + r);

            // checking for the rounds winner
            if (Math.abs(input - c) <= Math.abs(r - c)) {
                System.out.println("The winner is the user!");
                userscore++;
            } else {
                System.out.println("The winner is the computer!");
                dealerscore++;
            }

            if (input == c) { // checking for ending the game
                System.out.println("================ GAME STATS ================");
                System.out.println("Thank you for playing.");
                System.out.println("Number of hands played: " + gamesplayed);
                System.out.println("Dealer score: " + dealerscore);
                System.out.println("User score: " + userscore);
                System.out.println("You are " + userscore + " out of " + gamesplayed + "!");
                System.out.println("============================================");
                sc.close();
                break;
            }

            gamesplayed++; // increment games played

            System.out.println("--------------------------------------------");
        }

    }

}