簡體   English   中英

從另一個 class 打印計數變量

[英]print count variable from another class

我在 Java 中創建了一個 RandomSumGame,需要提供輸贏數量的計數。 我試圖在方法中添加計數的打印,但它在每場比賽后打印出來,而不是在最后打印出來。 我將打印移動到主 class 但我無法讓它打印來自其他 class 的計數總數。
下面是我的程序

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

我確信我錯過了一些簡單的東西,但我似乎無法解決這個問題。

將 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) {

並使用 object 調用這些變量

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

另外,我建議您在示例中使用封裝( Getters 和 Setters ),您將變量定義為私有並編寫一個 getter 方法來訪問它

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

並調用它使用test.getCountL();

首先,您將 2 function 命名為“播放”。 但在那之后,您可以簡單地將您在主 function 中使用的這個播放 function 定義為具有 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]);
            
    }

你的 class

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

我會從這個項目中抽出一些時間來閱讀一些有關如何進行面向對象編程的內容。 Alan Kay 或 Allen Holub 的任何作品都是一個好的開始,盡管還有許多其他作品同樣清晰。 請不要關注有關 getter 和 setter 與封裝有關的建議。 他們也泄露了內部細節。 誠然,它們比公共領域稍微好一點,但你想盡量避免完全放棄 state。 當您精通 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 中的最大錯誤是什么。 他說“上課”。 我花了相當長的時間才理解那句話的細節,但我最終意識到他既是故意搞笑,又是完全正確的。

喬恩

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM