如何计算 Java 中 ArrayList 中特定项目的数量
How to count for an amount of specific items in an ArrayList in Java
所以我最近决定使用 Java 编程重新创建 "Dice 10,000" 的游戏,但我在使某些规则起作用时遇到了一些问题。有 4 条规则我无法上班。
1) 任意数中的 3 = 3 个骰子面值的 10 倍
2) 完整顺子 (1 - 6) = 3,000 点
3) 3 对 = 1,000 点
4) 3个=1000分
我只是不确定如何让这些工作...我相信我会使用集合,但我对集合以及如何使用它们以及它们包含的方法非常非常缺乏经验。我大概能算出第二个没有集合,但它是第一个、第三个和第四个给我带来最大麻烦的。
//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
}
}
抱歉,我知道现在很多。希望你们能给我一些示例代码让我继续?非常感谢。
一切顺利
安德鲁
编辑:截至目前,我要到明天早上才能回复。对于缺乏反馈,我深表歉意,但已经晚了。我明天会回来尝试和回复。
在下面,我添加一些评论。它打印 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");
}
}
}
prints:
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
所以我最近决定使用 Java 编程重新创建 "Dice 10,000" 的游戏,但我在使某些规则起作用时遇到了一些问题。有 4 条规则我无法上班。
1) 任意数中的 3 = 3 个骰子面值的 10 倍
2) 完整顺子 (1 - 6) = 3,000 点
3) 3 对 = 1,000 点
4) 3个=1000分
我只是不确定如何让这些工作...我相信我会使用集合,但我对集合以及如何使用它们以及它们包含的方法非常非常缺乏经验。我大概能算出第二个没有集合,但它是第一个、第三个和第四个给我带来最大麻烦的。
//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
}
}
抱歉,我知道现在很多。希望你们能给我一些示例代码让我继续?非常感谢。
一切顺利
安德鲁
编辑:截至目前,我要到明天早上才能回复。对于缺乏反馈,我深表歉意,但已经晚了。我明天会回来尝试和回复。
在下面,我添加一些评论。它打印 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");
}
}
}
prints:
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