如何在Java中计算ArrayList中特定项目的数量
因此,我最近决定使用Java编程重新创建“骰子10000”游戏,我在一些规则的执行方面遇到了一些问题。有四条规定我不能上班 1) 任意数字的3=3个骰子面值的10倍 2) A全直(1-6)=3000分 3) 3对=1000点 4) 3分=1000分 我只是不知道如何让这些工作。。。我相信我会使用集合,但我对集合以及如何使用它们以及它们包含的方法非常、非常缺乏经验。我可能不需要一套就可以算出第二套,但第一套、第三套和第四套给我带来了最大的麻烦如何在Java中计算ArrayList中特定项目的数量,java,arraylist,set,counting,Java,Arraylist,Set,Counting,因此,我最近决定使用Java编程重新创建“骰子10000”游戏,我在一些规则的执行方面遇到了一些问题。有四条规定我不能上班 1) 任意数字的3=3个骰子面值的10倍 2) A全直(1-6)=3000分 3) 3对=1000点 4) 3分=1000分 我只是不知道如何让这些工作。。。我相信我会使用集合,但我对集合以及如何使用它们以及它们包含的方法非常、非常缺乏经验。我可能不需要一套就可以算出第二套,但第一套、第三套和第四套给我带来了最大的麻烦 //Andrew M import java.util
//Andrew M
import java.util.*;
import java.io.*;
class Zilch
{
public static Random r = new Random();
public static ArrayList<Integer> diceList = new ArrayList<Integer>();
public static int count = 0, humanScore = 0, compScore = 0, humanBank = 0, compBank = 0, lastMove = 0;
public static ArrayList<Integer> diceKeep = new ArrayList<Integer>();
public static boolean gameOn = true, x = true;
//-----------------------------------------------------------------------------------
public static void main(String[] args)
{
for (int j = 1; j <= 6; j++)
{
diceList.add(j);
diceKeep.add(null);
}
do
{
do
{
x = true;
for (int k = 0; k < diceKeep.size(); k++)
{
if (diceKeep.get(k) != null)
{
System.out.println("Dice #" + (k + 1) + ": " + diceKeep.get(k) + " -- kept.");
}
}
System.out.println();
System.out.println();
boolean b = true;
for (int i = 0; i < diceList.size(); i++)
{
if(diceList.get(i) != 0)
{
diceList.set(i, r.nextInt(6) + 1);
System.out.println("Dice #" + (i+1) + " is a " + diceList.get(i));
}
}
System.out.println();
System.out.println("Select which dice to roll again... (Select one at a time, 1 - 6), then type \"go\" to complete your turn.");
Scanner input = new Scanner(System.in);
/*-----------UNNECCESARY CODE--------------*/
else
if (selection.equals("go"))
{
System.out.println();
System.out.println();
if (diceKeep.get(0) == 1)
{
humanBank = humanBank + 100;
lastMove = 100;
}
else
if (diceKeep.get(0) == 5)
{
humanBank = humanBank + 500;
lastMove = 500;
}
/*--------------I NEED THE RULES TO GO HERE---*/
b = false;
}
}
while(b == true);
//-----------------------------------------------------------------------
if(gameOn == true)
{
x = true;
//Place Score Card Here
}
}
while (x == true);
}
while (1< 2); //Just here for testing purposes, for now
}
}
//Andrew M
导入java.util.*;
导入java.io.*;
一等货
{
公共静态随机r=新随机();
public static ArrayList diceList=new ArrayList();
公共静态整数计数=0,humanScore=0,compScore=0,humanBank=0,compBank=0,lastMove=0;
public static ArrayList diceKeep=new ArrayList();
公共静态布尔gameOn=true,x=true;
//-----------------------------------------------------------------------------------
公共静态void main(字符串[]args)
{
对于(intj=1;j在下面,我添加了一些注释。它在arraylist中打印数字计数
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
int [] i = new int [7]; // array starts with 0, so we can init our array with size 7 (0 to 6).
ArrayList<Integer> arr = new ArrayList<Integer>();
// there are 2 times "5"
arr.add(5);
arr.add(5);
//add some null
arr.add(null);
// there is 1 times "3"
arr.add(3);
// there is 1 times "6"
arr.add(6);
//add some null
arr.add(null);
System.out.println("numbers in arr: " + arr);
for (Integer integer : arr) {
if (integer!= null)
i[integer]++;
}
for (int c = 0 ;c<i.length ; c++){
System.out.println("there are/is " + c + " ->" + i[c] + " times");
}
}
}
有很多方法可以做到这一点。如果是我,我会对配对使用Map
,其中键是骰子的编号,值是出现的次数。我想把它放在If语句中,就像If(/*有三个五*/)
另外,你能发布一个描述如何使用地图的答案吗?如果我的问题被它解决了,我会很乐意接受。但是现在,我要睡觉了。我明天会反馈。使用地图
你可以遍历这些值,看看它们中是否有一个大于6(三对),或者检查地图是否只有一个条目(完全正确)。我,我才刚开始,所以我不知道地图是如何工作的。我会考虑如何用手来做,而不考虑编程。例如,你怎么能检查规则1?我在想,看看有多少个骰子与第一个相同(看看计数是否为3(或更多?);然后查看2到6中有多少与第二个模具相同,3到6中有多少与第三个模具相同,4到6中有多少与第四个相同。两个循环(一个嵌套在另一个内部)我相信你能做到这一点。顺便说一下,请注意diceKeep
中的所有值在分配之前都是null
。因此,如果玩家保留骰子编号5,那么骰子编号1-4将null
直到“keep”。因此,我不能使用Collections.sort(diceKeep)或泡泡排序或选择排序(全部抛出一个NullPointerException
)
numbers in arr: [5, 5, null, 3, 6, null]
there are/is 0 ->0 times
there are/is 1 ->0 times
there are/is 2 ->0 times
there are/is 3 ->1 times
there are/is 4 ->0 times
there are/is 5 ->2 times
there are/is 6 ->1 times