Java 输出不正确

Java 输出不正确,java,arrays,Java,Arrays,因此,我们的任务如下: 您将对特定类型的纸牌进行建模。游戏以45张牌开始。(他们需要 不要玩扑克牌。没有标记的索引卡也可以。) 分成若干随机大小的桩。例如,您可以从成堆的 20号、5号、1号、9号和10号。在每一轮中,你从每一堆牌中取出一张牌,形成一张新的牌 把这些卡片堆起来。例如,示例启动配置为 转化为尺寸为19、4、8、9和5的桩。当纸牌堆满时,纸牌就结束了 大小1、2、3、4、5、6、7、8和9,按一定顺序排列。(可以看出,你总是以失败告终 有这样的配置。) 我们被授予SolitaireT

因此,我们的任务如下:

您将对特定类型的纸牌进行建模。游戏以45张牌开始。(他们需要 不要玩扑克牌。没有标记的索引卡也可以。) 分成若干随机大小的桩。例如,您可以从成堆的 20号、5号、1号、9号和10号。在每一轮中,你从每一堆牌中取出一张牌,形成一张新的牌 把这些卡片堆起来。例如,示例启动配置为 转化为尺寸为19、4、8、9和5的桩。当纸牌堆满时,纸牌就结束了 大小1、2、3、4、5、6、7、8和9,按一定顺序排列。(可以看出,你总是以失败告终 有这样的配置。)

我们被授予SolitaireTester课程,我将在下面提供:

public class SolitaireTester
{
public static void main(String[] args)
{
    Solitaire s = new Solitaire();

    System.out.println("Start: " + s.toString());

    int rounds = 0;
    while (!s.over()) {
        s.round();
        ++rounds;
        System.out.println(rounds + ": " + s.toString());
    }
    System.out.println("Rounds: " + rounds);
}
}
我们得写纸牌课。以下是我到目前为止的情况:

import java.util.*;
import java.math.*;

public class Solitaire
{
int size = 45;

private ArrayList piles;

//Constructor for the initialization of random values
public Solitaire()
{
    piles = new ArrayList<Integer>();

    for(int i = 0; i < size; i++)
    {
        int temp = (int)(Math.random()*10)+1;
        piles.add(temp);
    }
}

//toString() will return a string representation of a Solitaire object
public String toString()
{
    return piles.toString();
}

/*
 * over() will return true if the solitaire is over, false otherwise.  The solitaire is over when 
 * the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 9, in some order
 */
public boolean over()
{
    int sum = 0;

    int a[] = {1,2,3,4,5,6,7,8,9};

    Iterator itr = piles.iterator();

    while(itr.hasNext())
    {
        int check = (int) itr.next();

        for(int i = 0; i < 9; i++)
        {
            if(a[i] == check)
            {
                a[i] = 0;
            }
        }
    }

    if(sum == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//round() takes one card from each pile, forming a new pile with these cards
public boolean round()
{
    Iterator itr = piles.iterator();

    int count = 0;

    while(itr.hasNext())
    {
        int check = (int) itr.next();
        count += 1;
    }

    if(count == 9)
    {
        return true;
    }
    else
    {
        return false;
    }
}
}
实际输出应该有更多的回合,并实际给出正确的输出

如果有人能给我指出我犯错误的正确方向。此外,如果有任何无关的东西,我包括和不应该,那么这也将是有益的。求你了,我不是在寻找答案,只是在正确的方向上轻推一下。谢谢大家!

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;

public class Solitaire
{

int maxSize = 45;
int newPile;

private ArrayList<Integer> piles;

/*
 * Constructor for the initialization of random values.
 * Initialize piles to a random number of piles of random size, but exactly 45 cards total.
 */
public Solitaire()
{

    int totalNumberOfStartingPiles = (int) (Math.random() * 10) + 1;

    boolean continueLoop = true;
    int total = 0;
    int size = 45;

    while (continueLoop)
    {
        piles = new ArrayList<Integer>();

        for (int i = 0; i < totalNumberOfStartingPiles; i++)
        {
            int temp = getRandomPile(size - totalNumberOfStartingPiles + i);

            if (i == totalNumberOfStartingPiles - 1)
            {
                piles.add(size);
            } else {
                piles.add(temp);
                size = size - temp;
            }
        }

        for (int i = 0; i < piles.size(); i++)
        {
            total += piles.get(i);
        }

        if (total == maxSize)
        {
            continueLoop = false;
        }
    }
}

/*
 * Randomizes and returns the total number of starting piles.
 * 
 * @return Integer that is the total number of starting piles.
 */
public int getRandomPile(int size)
{
    int totalNumberOfStartingPiles = (int) (Math.random() * size) + 1;
    return totalNumberOfStartingPiles;
}

/* 
 * toString() will return a string representation of a Solitaire object.
 * 
 * @return String representation of a Solitaire object.
 */
public String toString()
{
    return piles.toString();
}

/*
 * over() will return true if the solitaire is over, false otherwise. The
 * solitaire is over when the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 
 * 9, in some order.
 * 
 * @return true if the solitaire is over, false otherwise.
 */
public boolean over()
{
    int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    boolean flag = true;

    for (int i = 0; i < piles.size(); i++)
    {
        if (a[i] != piles.get(i))
        {
            flag = false;
            break;
        }
    }

    return flag;
}

/*
 * In each round, you take one card from each pile, forming a new pile with these cards.
 * Removes a pile that reaches a size of 0 cards.
 */
public void round()
{
    newPile = piles.size();

    for (int i = 0; i < piles.size(); i++)
    {
        piles.set(i, (piles.get(i)) - 1);

        if(piles.get(i)==0)
        {
            piles.remove(i);
            i--;
        }
    }

    piles.add(newPile);
    Collections.sort(piles); //sorts the pile into ascending numerical order
}
导入java.util.Collections; 公营单人纸牌 { int maxSize=45; int新桩; 私人ArrayList桩; /* *用于初始化随机值的构造函数。 *将堆初始化为随机数量的随机大小的堆,但总共45张卡。 */ 公共纸牌 { int totalNumberOfStartingPiles=(int)(Math.random()*10)+1; 布尔连续运算=真; int-total=0; int size=45; while(continueLoop) { 桩=新的ArrayList(); 对于(int i=0;i

}

如果您知道您的代码应该生成什么,并且您可以看到实际输出是不同的,那么您应该能够反向工作,以了解您的期望与现实之间的差异。您能提供更多帮助吗?非常感谢。在寻求帮助之前,您需要学习如何解决自己的问题,并至少确定代码哪里出了问题。现在,您要求人们花时间学习您的所有代码,然后为您正确地重新编写代码--我们在这里不这样做。感谢您的帮助,或者您的帮助不足。请只输入更改的部分,而不是完整的、更正的程序。请描述它是如何修复的,并给出一个示例输出。问题是我必须从头开始重写代码,因为我最初的尝试实际上是垃圾。更正后的代码与原始代码有很大的不同,因此实际上没有一种方法来记录所有的差异。原始代码没有产生任何输出的外观。更正后的代码的输出类似于:Start:[11,12,22]1:[10,11,21,3]2:[9,10,20,2,4]3:[8,9,19,1,3,5]4:[7,8,18,2,4,6]5:[6,7,17,1,3,5,6]6:[5,6,16,2,4,5,5,5,7]7:[4,4,14,2,3,5,6,6,8]9:[2,3,13,1,2,5,5,5,7,7,7]。55:[1,2,4,4,4,6,7,8,9]56:[1,3,3,5,6,7,8,9]57:[2,2,2,4,5,6,7,8,9]58:[1,1,1,3,4,5,6,7,8,9]59:[2,4,5,7,5,8,10]60:[1,2,3,4,5,6,7,9,8]轮:60然而,我编写的程序将按升序对每一堆进行排序,然后再由管理员检查()方法。那么请把它写在答案里。另外,记住这一点:我们倾向于劝阻人们不要认为SO会为他们编写代码,所以这可能不是SO的好答案。
import java.util.ArrayList;
import java.util.Collections;

public class Solitaire
{

int maxSize = 45;
int newPile;

private ArrayList<Integer> piles;

/*
 * Constructor for the initialization of random values.
 * Initialize piles to a random number of piles of random size, but exactly 45 cards total.
 */
public Solitaire()
{

    int totalNumberOfStartingPiles = (int) (Math.random() * 10) + 1;

    boolean continueLoop = true;
    int total = 0;
    int size = 45;

    while (continueLoop)
    {
        piles = new ArrayList<Integer>();

        for (int i = 0; i < totalNumberOfStartingPiles; i++)
        {
            int temp = getRandomPile(size - totalNumberOfStartingPiles + i);

            if (i == totalNumberOfStartingPiles - 1)
            {
                piles.add(size);
            } else {
                piles.add(temp);
                size = size - temp;
            }
        }

        for (int i = 0; i < piles.size(); i++)
        {
            total += piles.get(i);
        }

        if (total == maxSize)
        {
            continueLoop = false;
        }
    }
}

/*
 * Randomizes and returns the total number of starting piles.
 * 
 * @return Integer that is the total number of starting piles.
 */
public int getRandomPile(int size)
{
    int totalNumberOfStartingPiles = (int) (Math.random() * size) + 1;
    return totalNumberOfStartingPiles;
}

/* 
 * toString() will return a string representation of a Solitaire object.
 * 
 * @return String representation of a Solitaire object.
 */
public String toString()
{
    return piles.toString();
}

/*
 * over() will return true if the solitaire is over, false otherwise. The
 * solitaire is over when the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 
 * 9, in some order.
 * 
 * @return true if the solitaire is over, false otherwise.
 */
public boolean over()
{
    int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    boolean flag = true;

    for (int i = 0; i < piles.size(); i++)
    {
        if (a[i] != piles.get(i))
        {
            flag = false;
            break;
        }
    }

    return flag;
}

/*
 * In each round, you take one card from each pile, forming a new pile with these cards.
 * Removes a pile that reaches a size of 0 cards.
 */
public void round()
{
    newPile = piles.size();

    for (int i = 0; i < piles.size(); i++)
    {
        piles.set(i, (piles.get(i)) - 1);

        if(piles.get(i)==0)
        {
            piles.remove(i);
            i--;
        }
    }

    piles.add(newPile);
    Collections.sort(piles); //sorts the pile into ascending numerical order
}