Java 在每个程序运行时实现一个静态计数器

Java 在每个程序运行时实现一个静态计数器,java,Java,我正在做一个简单的lucky 9纸牌游戏,我希望计数器增加,这样我就可以记录每个玩家的赢数,我已经声明winCount为静态,但每次我点击run程序,计数器输出1或0 import java.util.Random; public class Tester { static int p1WinCount; static int p2WinCount; public static void main(String[] args) { Random rand = new Random(

我正在做一个简单的lucky 9纸牌游戏,我希望计数器增加,这样我就可以记录每个玩家的赢数,我已经声明winCount为静态,但每次我点击run程序,计数器输出1或0

import java.util.Random;

public class Tester {

static int p1WinCount;
static int p2WinCount;

public static void main(String[] args) {
    Random rand = new Random();
    Tester test = new Tester();

    String player1 = "PLAYER 1";
    int p1r1 = rand.nextInt(13)+1;
    int p1s1 = rand.nextInt(4)+1;
    int p1r2 = rand.nextInt(13)+1;
    int p1s2 = rand.nextInt(4)+1;
    int p1total = test.total(test.convRank1(p1r1), test.convRank2(p1r2));

    System.out.println(player1);
    System.out.printf("Card 1: %s of %s\n", test.getRank(p1r1), test.getSuit(p1s1));
    System.out.printf("Card 2: %s of %s\n", test.getRank(p1r2), test.getSuit(p1s2));
    System.out.printf("Card Total: %d\n", p1total);

    System.out.println();

    String player2 = "PLAYER 2";
    int p2r1 = rand.nextInt(13)+1;
    int p2s1 = rand.nextInt(4)+1;
    int p2r2 = rand.nextInt(13)+1;
    int p2s2 = rand.nextInt(4)+1;
    int p2total = test.total(test.convRank1(p2r1), test.convRank2(p2r2));

    System.out.println(player2);
    System.out.printf("Card 1: %s of %s\n", test.getRank(p2r1), test.getSuit(p2s1));
    System.out.printf("Card 2: %s of %s\n", test.getRank(p2r2), test.getSuit(p2s2));
    System.out.printf("Card Total: %d\n", p2total);

    System.out.println();

    if(p1total > p2total) {
        System.out.println("PLAYER 1 IS THE WINNER");
        p1WinCount++;
    } else {
        System.out.println("PLAYER 2 IS THE WINNER");
        p2WinCount++;
    }

    System.out.println();
    System.out.printf("Player 1 wins %d times / Player 2 wins %d times", p1WinCount, p2WinCount);
}

public String getRank(int x) {
    switch(x) {
    case 1:
        return "ACE";
    case 2:
        return "TWO";
    case 3:
        return "THREE";
    case 4:
        return "FOUR";
    case 5:
        return "FIVE";
    case 6:
        return "SIX";
    case 7:
        return "SEVEN";
    case 8:
        return "EIGHT";
    case 9:
        return "NINE";
    case 10:
        return "TEN";
    case 11:
        return "JACK";
    case 12:
        return "QUEEN";
    case 13:
        return "KING";
    } return null;
}

public String getSuit(int x) {
    switch(x) {
    case 1:
        return "DIAMOND";
    case 2:
        return "CLUBS";
    case 3:
        return "HEARTS";
    case 4:
        return "SPADE";
    } return null;
}

public int convRank1(int x) {
    if(x > 9) {
        return x - 9;
    } else {
        return x;
    }
}

public int convRank2(int x) {
    if(x > 9) {
        return x - 9;
    } else {
        return x;
    }
}

public int total(int x, int y) {
    if(x + y > 9) {
        return x + y - 9;
    } else {
        return x + y;
    }
}
}

以下是输出:

PLAYER 1
Card 1: FIVE of SPADE
Card 2: QUEEN of SPADE
Card Total: 8

PLAYER 2
Card 1: FOUR of SPADE
Card 2: ACE of CLUBS
Card Total: 5

PLAYER 1 IS THE WINNER

Player 1 wins 1 times / Player 2 wins 0 times

您需要将计数存储/持久化到文件或数据库(磁盘内存),仅声明静态计数器不会在多次运行中保留数据


静态计数器(实际上是所有程序变量)仅在程序执行(运行)期间位于主存(RAM)中,并且在程序运行终止后不可用。因此,当您再次启动程序时,它将是一次新的运行,您无法获取以前的运行数据。

您需要将计数存储/持久化到文件或数据库(磁盘内存),仅声明静态计数器不会在多次运行时保留数据。


静态计数器(实际上是所有程序变量)仅在程序执行(运行)期间位于主存(RAM)中,并且在程序运行终止后不可用。因此,当您再次启动程序时,它将是一次新的运行,您无法获取以前的运行数据。

程序完成后,静态变量将被清除。您必须让您的程序将每个玩家的赢数写入一个文件,或者在有人赢后继续运行(添加重新启动按钮)。静态变量不会持续超过程序的生命周期,它们只是不属于对象。

一旦程序完成,静态变量将被清除。您必须让您的程序将每个玩家的赢数写入一个文件,或者在有人赢后继续运行(添加重新启动按钮)。静态变量不会持续超过程序的生命周期,它们只是不属于对象。

您可以参考:一本好书。。谢谢..你可以参考:一本好书。。谢谢..哦,不,我离I/O还有几章,我现在就放弃赢数,等我看完I/O一章后再回来,谢谢你的回答,非常感谢..哦,不,我离I/O还有几章,现在,我将放弃赢球计数,在我完成i/O章节后再回到赢球计数,谢谢您的回答,非常感谢。。