我用什么来记录数字?

时间:2017-01-17 20:41:42

标签: java arrays loops

SELECT 
    table1.productid, table1.product, table1.catogory, 
    table2.productid, table2.product, table2.catogory
FROM 
    table1
JOIN
    (SELECT 
         table1.productid, table1.product, table1.catogory
     FROM 
         table1 
     WHERE 
         table1.productid = 12345 
         AND table1.product = 'USB Drive') AS table2 ON table1.product = table2.product 
                                                     AND table1.productid = table2.productid
WHERE 
    table1.productid = 12345 
    AND table1.product = 'USB Drive'

我接受了一些事情,但我放弃了。如何跟踪选择两个骰子总和的次数? 一会儿循环? (两个模具的结果只能在2到12之间)

因此,例如,我想要一个循环,将一个加到其计数器中为其分配的数字

x =两个死的总和

y =次数

输出可以是“数字x被滚动y次”

2 个答案:

答案 0 :(得分:1)

在你的while循环中,你只需要另一个数组来保存每个可能的滚动计数。

你已经获得了总数,就在这里。

int total = dice1 + dice2;

只需在循环外添加一个数组即可开始计数:

int possibleRolls[]=new int[13]

并在你的循环中:

possibleRolls[total]++;

当循环结束时,您可以检查数组中的索引以查看每个循环的总数。

答案 1 :(得分:0)

您可以创建一个地图,该地图是数字的地图到滚动的次数。

java.util.Map<Integer, Integer> rollMap = new java.util.HashMap<Integer, Integer>();
for (int i = 0; i < rollamt; i++)
{
    int dice1 = gen.nextInt(6) + 1;
    int dice2 = gen.nextInt(6) + 1; 
    int total = dice1 + dice2;

    Integer count = rollMap.get(total);
    if (null == count)
    {
        rollMap.put(total, Integer.valueOf(1));
    }
    else
    {
        rollMap.put(total, Integer.valueOf(count + 1));
    }
}

for (Map.Entry<Integer, Integer> entry : rollMap.entrySet())
{
    System.out.println("Number " + entry.getKey()+ " was rolled " + entry.getValue() + "times")
}