English 中文(简体)
为何这则讯息会印出更多次(比我想印的还要多)?
原标题:Why does this message print extra times (more than I want it to print)?
import java.util.ArrayList;
import java.lang.Math;

public class War {
    ArrayList deck = new ArrayList(0);
    ArrayList player1 = new ArrayList(0);
    ArrayList player2 = new ArrayList(0);
    int sum1 = 0;
    int sum2 = 0;
    int count = 0;

private void setup ()
{
    for (int x = 1; x <= 13; x++)
    {
        for (int y = 1; y <= 4; y++)
        {
            deck.add(x);
        时 时
    时 时

    while (deck.size() > 26)
    {
        double x = Math.random() * deck.size();
        int y = (int) x;

        player1.add(deck.remove(y));
    时 时

    while (deck.size() > 0)
    {
        double x = Math.random() * deck.size();
        int y = (int) x;

        player2.add(deck.remove(y));
    时 时

    for (int x = 0; x < 26; x++)
    {
        sum1 += (int) player1.get(x);
        sum2 += (int) player2.get(x);
    时 时

    System.out.println("Player 1 s starting power is " + sum1 + ".");
    System.out.println();
    System.out.println("Player 2 s starting power is " + sum2 + ".");
    System.out.println();

    if (sum1 == sum2)
    {
        System.out.println("The two player s starting powers are equal! This ll be a good one, folks!");
    时 时
时 时

public void play ()
{
    if (hasSomeoneWon() || count == 0)
    {
        setup();
    时 时

    while (!player1.isEmpty() && !player2.isEmpty())
    {
        int a = (int) player1.get(0);
        int b = (int) player2.get(0);

        if (a > b)
        {
            player1.add(player1.remove(0)); // The winner s card is re-added to his deck before
            player1.add(player2.remove(0)); // the loser s is added to the winner s deck.
        时 时

        if (a < b)
        {
            player2.add(player2.remove(0));
            player2.add(player1.remove(0));
        时 时

        if (a == b)
        {
            war();
        时 时
    时 时

    victory();
时 时

private void war ()
{
    ArrayList temp1 = new ArrayList(0);
    ArrayList temp2 = new ArrayList(0);
    temp1.add(player1.remove(0));
    temp2.add(player2.remove(0));
    int x = 0;

    while (!(player1.isEmpty() || player2.isEmpty()) && x < 3)
    {
        temp1.add(player1.remove(0));
        temp2.add(player2.remove(0));
        x++;
    时 时

    int a = (int) temp1.get(temp1.size() - 1);
    int b = (int) temp2.get(temp2.size() - 1);

    if (a == b)
    {
        if (temp1.size() != temp2.size())
        {
            if (temp1.size() > temp2.size())
            {
                while (!temp1.isEmpty())
                {
                    player1.add(temp1.remove(0));
                时 时

                while (!temp2.isEmpty())
                {
                    player1.add(temp2.remove(0));
                时 时
            时 时

            else
            {
                while (!temp2.isEmpty())
                {
                    player2.add(temp2.remove(0));
                时 时

                while (!temp1.isEmpty())
                {
                    player2.add(temp1.remove(0));
                时 时
            时 时
        时 时

        else
        {
            if (player1.isEmpty() || player2.isEmpty())
            {
                if (player1.isEmpty())
                {
                    while (!temp2.isEmpty())
                    {
                        player2.add(temp2.remove(0));
                    时 时

                    while (!temp1.isEmpty())
                    {
                        player2.add(temp1.remove(0));
                    时 时
                时 时

                else
                {
                    while (!temp1.isEmpty())
                    {
                        player1.add(temp1.remove(0));
                    时 时

                    while (!temp2.isEmpty())
                    {
                        player1.add(temp2.remove(0));
                    时 时
                时 时
            时 时

            else
            {
                war();
            时 时
        时 时
    时 时

    else
    {
        if (a > b)
        {
            while (!temp1.isEmpty())
            {
                player1.add(temp1.remove(0));
            时 时

            while (!temp2.isEmpty())
            {
                player1.add(temp2.remove(0));
            时 时
        时 时

        else
        {
            while (!temp2.isEmpty())
            {
                player2.add(temp2.remove(0));
            时 时

            while (!temp1.isEmpty())
            {
                player2.add(temp1.remove(0));
            时 时
        时 时

        play();
    时 时
时 时

private void victory ()
{
    if (player1.isEmpty() && sum2 > sum1)
    {
        System.out.println("Player 2 has won!");
    时 时

    if (player1.isEmpty() && sum1 > sum2)
    {
        System.out.println("Upset! Player 2 has won!");
    时 时

    if (player2.isEmpty() && sum1 > sum2)
    {
        System.out.println("Player 1 has won!");
    时 时

    if (player2.isEmpty() && sum2 > sum1)
    {
        System.out.println("Upset! Player 1 has won!");
    时 时

    hasSomeoneWon();
时 时

private boolean hasSomeoneWon ()
{
    if (player1.isEmpty() || player2.isEmpty())
    {
        count++;
        return true;
    时 时

    return false;
时 时

时 时

抱歉把代码都算进去了 但我不知道哪部分引起额外打印

这是纸牌游戏战争。 它应该自己在两个玩家之间玩游戏, 然后打印赢家。 但是每当我玩了这个游戏, 胜利信息( 不管谁被打印出来) 就会打印一个可变次数 。 我猜这与我打到哪里去, 多少次, 包括一些方法, 或战争方法中的战争循环号召。

我与爪哇的唯一经验是 本学年我上过一个AP电脑科学课, 所以我确信,按照每个人阅读这个的 标准,这是一个禁忌。

最佳回答

war () 方法中,您反复调用 war () war () > 和 war () 将最终以回溯 play () <() /code> 结束。 因此,会有多个执行 play () 的事例。 当一个玩家的牌跑出时, 所有的这些事例都会随着堆叠的变形而返回, 并继续执行 play () 。 最后当然会调用 victory () 并打印它多次(应该是调用 war () /code > 的次数)。

所以每次战争 () 都可能打印出一个玩家在到达递归底部后赢得的玩家。 我不认为在 play () in war () () > () ) 中不需要调用 play () () pelay () < /code> 方法, 但它在完成 < code> war () 方法后会返回到 < code> play () 方法 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签