以下代码显示了一个简单的五个骰子。
如果任何两个骰子匹配,而另外三个骰子也匹配,我想给出卷的总和。但是,我不太确定在使用if语句时如何表示。
示例:4 4 3 4 3
此外,如果骰子的值可以按连续的环绕顺序排列,我想弄清楚相同的情况,(1跟随6后)。
示例:6 1 2 3 4
任何帮助或建议将不胜感激,谢谢!
int die1;
int die2;
int die3;
int die4;
int die5;
int roll;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
die3 = (int)(Math.random()*6) + 1;
die4 = (int)(Math.random()*6) + 1;
die5 = (int)(Math.random()*6) + 1;
System.out.print("The roll was : " + die1 + " " + die2 + " " + die3 + " " + die4 + " " + die5 + " ");
答案 0 :(得分:0)
将您的卷放入ArrayList<Integer>
会让您更轻松。假设我们有,我们可以使用Stream
:
Collection<Long> counts = rollList.stream() //Stream<Integer>
.collect(Collectors.groupingBy( //Group to Map
Function.identity(), //Key = The roll itself
Collectors.counting())) //Value = How many were rolled
.values() //Values only
if(counts.contains(2) && counts.contains(3)) { //One rolled twice, other thrice
int total = rollList.stream().mapToInt(Integer::intValue).sum(); //Sum of all rolls
System.out.println("You rolled a full house with a total value of "+total);
else if(counts.size() == 5) //If all rolls were different, it was definitely a straight because there are 6 wrapping values and 5 rolls
System.out.println("You rolled a straight");
我认为您也可以直接将值放入IntStream
。