【问题标题】:Problem with keeping score with a while loop (Java)使用while循环保持分数的问题(Java)
【发布时间】:2019-03-07 00:16:38
【问题描述】:

我正在介绍 java 类,对于我的一项作业,我必须使用循环(for 或 while)来跟踪我自己和计算机之间的分数。

这是我教授的逐字说明:

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

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

游戏轮数:5 经销商获胜:3 玩家获胜:2

5 比 2。

现在我已经编写了代码并玩了好几个小时,但无法让它在循环中工作。我试过while,do while,for。我在互联网上到处寻找类似的例子,但无论如何都无法在我的程序中使用循环。如果有人有任何建议,我一定会感谢您的反馈。

我的代码:

    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
}

一切正常,但我无法让循​​环在这里的任何地方工作。

【问题讨论】:

  • 您的while 循环只包含一行代码gamesplayed++;,因此它将永远循环变量增加。您需要更改循环以包含您的游戏逻辑。

标签: java loops for-loop while-loop


【解决方案1】:

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

然后在循环内部,不断询问用户输入。另外,您在添加分数时混淆了userscoredealerscore

最后,一旦你退出循环,你就可以打印分数/统计数据。

请阅读下面的cmets:

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

【讨论】:

  • 这将在用户输入非数字输入时生成java.util.InputMismatchException
  • 如果异常处理在OP的问题范围内,他们可以指定。他们似乎还没有达到课程的那部分
  • 不过,您应该尝试告诉 OP 可能的无效输入,因为我们知道得更清楚。并让 OP 决定他/她是否会实施它。没有冒犯的意思。 :)
【解决方案2】:

改变循环
      while (input != c && r != c){
          gamesplayed++;
          System.out.println("Games played: " + gamesplayed );
      }

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

【讨论】:

  • 我明白了。 OP 说问题在于循环没有工作,他们不知道它实际上在工作,没有被要求修复整体游戏实现。
  • 另一件事是,这是家庭作业的一部分,我帮助解决痛点总比给出答案要好。 @MC10
  • 循环工作,但不是它应该如何工作。例如,当我输入一个数字时,它会无限循环。为此,我需要一种简单的方法来跟踪分数。我遇到的另一个问题是在我运行代码之后,代码执行并结束。即使它确实想要跟踪分数,在代码完成运行后它也不会再接受任何输入。我必须再次运行代码,这会删除之前的分数。
【解决方案3】:

试试这个。说明见代码即可。

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

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2020-11-25
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多