【问题标题】:print count variable from another class从另一个类打印计数变量
【发布时间】:2020-11-27 01:45:51
【问题描述】:

我用 Java 创建了一个 RandomSumGame,需要提供输赢的数量。我试图在方法中添加计数的打印,但它在每场比赛后打印出来,而不是在最后打印出来。我将打印件移到了主类,但我无法让它打印其他类的总数。
下面是我的程序

package Question4;

import java.util.Random;

public class RandomSumGame {

    boolean start;
    int d1;
    int d2;
    int sum = d1 + d2;
    int valuePoint;
    String status = "+";
        
    public void play (int d1, int d2) {
        sum = d1 + d2;
        int countW = 0;
        int countL = 0;
        for (;;) { //Part I. of question
            if (sum == 2 || sum == 3 || sum == 12) {
                start = false;
                System.out.println(" = " + sum + "\nCRAPS! You lose!");
                countL++;
                break;
            }
            else if (sum == 7 || sum == 11) { //Part II. of question
                start = true;
                System.out.println(" = " + sum + "\nNATURAL! You win!");
                countW++;
                break;
            }
            
            valuePoint = sum;
            
            if (valuePoint == 4 ||valuePoint == 5 || valuePoint == 6 || valuePoint == 8 
                    || valuePoint == 9 || valuePoint == 10) { //Part III. of question
                System.out.println(" = " + valuePoint + "\nA value point of " + valuePoint 
                                        + " has been established. Roll again.");
                break;
            }
        }
        
        for (;;) {
            if (sum == 7) { //exception to valuepoint is 7
                break;
            }
            else {
                Random rand = new Random();
                for (int i = 0; i < 1; i++) {
                    d1 = 1 + rand.nextInt(6);
                    int a1 = d1;
                    System.out.print("You rolled\n" + "\t" + d1);
                    
                    for (int x = 0; x < 1; x++) {
                        d2 = 1 + rand.nextInt(6);
                        int a2 = d2;
                        int sum = a1 + a2;
                        System.out.println(" " + status + " " + d2 + " = " + sum);  
                    }   
                }
                
                sum = d1 + d2;
                
                if (sum == valuePoint) { //if sum equals valuepoint established
                    start = true;
                    System.out.print("\t" + "You win!" + "\n");
                    countW++;
                    break;
                }
                
                else if (sum == 7) {
                    start = false;
                    System.out.print("\t" + "You lose! " + "\n");
                    countL++;
                    break;
                }
            }
        }
    }
    
    public void play () {
        for (int x = 0; x < 3; x++) {
        rolldice();
        play(d1, d2);
        System.out.print("\n");
        }  
    }

    public void rolldice() {
        Random rand = new Random();

        for (int i = 0; i < 1; i++) {
            d1 = 1 + rand.nextInt(6);
            System.out.print("You rolled " + d1 + " " + status);
        }
        for (int i = 0; i < 1; i++) {
            d2 = 1 + rand.nextInt(6);
            System.out.print(" " + d2 + " ");
        }   
    }
    
}//end of class

这是我的主线

package Question4;

public class TestRandomSumGame {

    public static void main(String[] args) {
    
        
        System.out.println("Craps is a dice game where two dice are rolled."
                + " The dice are added together and; \nIf the sum is 7 or 11, you win!"
                + "\nIf the sum is 2, 3, or 12, you lose!"
                + "\nIf the sum is 4, 5, 6, 8, 9, or 10, a valuepoint is established which is"
                + "\nwhere you contine to roll the dice until you roll the same number"
                + " (you win)"
                + "\nor a 7 (you lose)."
                + "\nThe game will run for 3 plays.\n");
        
            RandomSumGame test = new RandomSumGame();
            
            test.play();
            
            
            System.out.println("Wins: " + countW + "\nLosses: " + countL);
            
    }
    
}//end of class

我确定我遗漏了一些简单的东西,但我似乎无法解决这个问题。

【问题讨论】:

  • 你不在这里错过test吗? System.out.println("Wins: " + test.countW + "\nLosses: " + test.countL);。考虑到这些属性是公开的。

标签: java class variables printing


【解决方案1】:

将 countW 和 countL 定义为全局变量,因为您将它们用作仅在 play() 方法中可用的局部变量。所以不是这个

public void play (int d1, int d2) {
    int countW = 0;
    int countL = 0;

使用类似的东西

int countW = 0;
int countL = 0;
public void play (int d1, int d2) {

并使用对象调用这些变量

    System.out.println("Wins: " + test.countW + "\nLosses: " + test.countL);

我还建议您使用封装(Getters 和 Setters) 对于您的示例,您将变量定义为私有并编写一个 getter 方法来访问它 f.e

private int countL = 0;
public void getCountL(){
    return this.countL;
}

并称之为使用 test.getCountL();

【讨论】:

  • 谢谢我将变量设置为全局变量。我知道这是我缺少的一点。
  • @ekelly 很高兴它有帮助,将此标记为答案也会对我有帮助:D 谢谢
【解决方案2】:

首先,您将 2 个函数命名为“play”。 但在那之后,您可以简单地将您在主函数中使用的这个播放函数定义为具有 int 数组类型的返回函数。 固定代码。

主要

public class TestRandomSumGame {

    public static void main(String[] args) {
    
        
        System.out.println("Craps is a dice game where two dice are rolled."
                + " The dice are added together and; \nIf the sum is 7 or 11, you win!"
                + "\nIf the sum is 2, 3, or 12, you lose!"
                + "\nIf the sum is 4, 5, 6, 8, 9, or 10, a valuepoint is established which is"
                + "\nwhere you contine to roll the dice until you roll the same number"
                + " (you win)"
                + "\nor a 7 (you lose)."
                + "\nThe game will run for 3 plays.\n");
        
            RandomSumGame test = new RandomSumGame();
            
            int[] outcome = test.play();
            
            
            System.out.println("Wins: " + outcome[0] + "\nLosses: " + outcome[1]);
            
    }

你的班级

package Question4;

import java.util.Random;

public class RandomSumGame {

    boolean start;
    int d1;
    int d2;
    int sum = d1 + d2;
    int valuePoint;
    String status = "+";
        
    public int[] start_game (int d1, int d2) {
        sum = d1 + d2;
        int countW = 0;
        int countL = 0;
        for (;;) { //Part I. of question
            if (sum == 2 || sum == 3 || sum == 12) {
                start = false;
                System.out.println(" = " + sum + "\nCRAPS! You lose!");
                countL++;
                break;
            }
            else if (sum == 7 || sum == 11) { //Part II. of question
                start = true;
                System.out.println(" = " + sum + "\nNATURAL! You win!");
                countW++;
                break;
            }
            
            valuePoint = sum;
            
            if (valuePoint == 4 ||valuePoint == 5 || valuePoint == 6 || valuePoint == 8 
                    || valuePoint == 9 || valuePoint == 10) { //Part III. of question
                System.out.println(" = " + valuePoint + "\nA value point of " + valuePoint 
                                        + " has been established. Roll again.");
                break;
            }
        }
        
        for (;;) {
            if (sum == 7) { //exception to valuepoint is 7
                break;
            }
            else {
                Random rand = new Random();
                for (int i = 0; i < 1; i++) {
                    d1 = 1 + rand.nextInt(6);
                    int a1 = d1;
                    System.out.print("You rolled\n" + "\t" + d1);
                    
                    for (int x = 0; x < 1; x++) {
                        d2 = 1 + rand.nextInt(6);
                        int a2 = d2;
                        int sum = a1 + a2;
                        System.out.println(" " + status + " " + d2 + " = " + sum);  
                    }   
                }
                
                sum = d1 + d2;
                
                if (sum == valuePoint) { //if sum equals valuepoint established
                    start = true;
                    System.out.print("\t" + "You win!" + "\n");
                    countW++;
                    break;
                }
                
                else if (sum == 7) {
                    start = false;
                    System.out.print("\t" + "You lose! " + "\n");
                    countL++;
                    break;
                }
            }
        }
       int[] outcome = {countW, countL}
       return outcome
    }
    
    public int[] play () {
        for (int x = 0; x < 3; x++) {
        rolldice();
        int[] outcome = start_game(d1, d2);
        System.out.print("\n");
        }
        return outcome
    }

    public void rolldice() {
        Random rand = new Random();

        for (int i = 0; i < 1; i++) {
            d1 = 1 + rand.nextInt(6);
            System.out.print("You rolled " + d1 + " " + status);
        }
        for (int i = 0; i < 1; i++) {
            d2 = 1 + rand.nextInt(6);
            System.out.print(" " + d2 + " ");
        }   
    }
    
}

【讨论】:

  • 谢谢,但这是一个作业,参数提供给我。
【解决方案3】:

我会从这个项目中抽出一些时间来阅读一些有关如何进行面向对象编程的内容。 Alan Kay 或 Allen Holub 的任何作品都是一个好的开始,尽管还有许多其他作品同样清晰。请不要关注有关 getter 和 setter 与封装有关的建议。他们也泄露了内部细节。诚然,它们比公共领域略好,但你想尽量避免完全给出状态。当您擅长 OO 编程时,您会发现 90% 以上的时间都可以避免 getter 和 setter(上帝禁止任何类型的暴露字段)。结果是您的代码更好、更快、更解耦、更易于维护等。有关快速文章,请阅读以下内容:

https://www.infoworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html

如果你没有时间看这篇文章,从那篇文章中了解 OO 编程的最重要的事情基本上是这样的:

It's not about objects, it's about conversations, and in those converstations you don't ask an object for its state to do some work, you ask the object with the state to do the work for you.

Alan Kay 说他认为将其称为面向对象编程是一个错误。如果它被称为面向消息的编程会更好。我同意。

顺便问一下,James Gosling 被问到他认为自己在 Java 中最大的错误是什么。他说“上课”。我花了相当长的时间才理解那句话的细节,但我最终意识到他既是故意搞笑,又是完全正确的。

乔恩

【讨论】:

  • 感谢您的建议。
  • 不客气。如果你真的去这样做,我可能会在你的职业发展中为你节省很多年。这么少的人真的会去做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
相关资源
最近更新 更多