在arraylist bound(Homework)方面需要帮助

时间:2016-03-31 18:55:22

标签: java arraylist

  

从包中取出2个豆子。

     

如果:

     
      
  • 他们都是黑色的,将其中一个放回包里丢弃   另一个。

  •   
  • 一个是黑色,另一个白色将白豆放回袋子里   并丢弃黑豆

  •   
  • 两者都是白色丢弃,并将黑豆放入袋中

  •   
     

最后一个豆是什么颜色的?

     

您决定编写一个可以运行50次模拟的程序。   从一袋10豆开始,继续运行程序   将袋子中的豆子数量增加1,直到你有   测试了一个60豆的袋子。用随机颜色填充袋子   豆类和随机抽取豆子。

     

运行模拟并打印出四列数字:

     
      
  1. 豆数
  2.   
  3. 黑豆数量
  4.   
  5. 白豆数量
  6.   
  7. 最后一个豆子的颜色。
  8.         

    使用ArrayList类来实现此程序。从中删除豆子   数组中的随机位置,并以不同的方式添加bean   随机位置

这里到目前为止我做了什么: 但它一直说arraylist是出界的。我知道问题出在哪里,但我不知道如何解决它。如果我从行中删除加1

index1 = rand.nextInt(bag.size() + 1)

它会说负面的错误。

import java.io.*; 
import java.util.*; 
import java.util.Random;
public class Counter
{
    public static void main (String args[])
    {
        ArrayList<Integer> bag = new ArrayList<Integer>();

        Random rand = new Random();

        int color;
        int index1;
        int index2;
        int blackCount = 0;
        int whiteCount = 0;
        String last = "";

        System.out.println("Beans     Black     White     Last");

        for (int total = 10; total <= 60; total++)
        {
            for (int run = total; run > 0; run--)
            {
                // 1 black and 2 white
                color = rand.nextInt(2) + 1;
                if (color == 1)
                {
                    blackCount++;
                }
                else if (color == 2)
                {
                    whiteCount++;
                }

                bag.add(rand.nextInt(bag.size() + 1), color);
            }

            System.out.print(total + "\t " + blackCount + "\t " + whiteCount + "\t ");     

            while (blackCount != 1 || whiteCount != 1)
            {
                index1 = rand.nextInt(bag.size());
                index2 = rand.nextInt(bag.size());


                if (bag.get(index1) == 1 && bag.get(index2) == 1)
                {
                    bag.remove(index2);
                    blackCount--;
                }
                else if (bag.get(index1) == 1 && bag.get(index2) == 2)
                {
                    bag.remove(index1);
                    blackCount--;
                }
                else if (bag.get(index1) == 2 && bag.get(index2) == 1)
                {
                    bag.remove(index2);
                    blackCount--;
                }
                else if (bag.get(index1) == 2 && bag.get(index2) == 2)
                {
                    bag.remove(index1);

                    index2 = rand.nextInt(bag.size());
                    while (bag.get(index2) == 1)
                    {
                        if (bag.get(index2) == 1)
                        {
                            index2 = rand.nextInt(bag.size());
                        }
                    }

                    bag.remove(index2);
                    bag.add(rand.nextInt(bag.size() + 1), 1);
                    blackCount++;
                    whiteCount -= 2;
                }
            }

            if (whiteCount == 1)
            {
                last = "White";
            }
            else 
            {
                last = "Black";
            }

            System.out.print(last + "\n");

            bag.clear();
            blackCount = 0;
            whiteCount = 0;
        }
    }
}

0 个答案:

没有答案
相关问题