骰子滚动数组和方法调用

时间:2017-06-12 18:48:26

标签: java

我正在研究这个问题,我无法弄清楚如何获取一个数组来跟踪main中的roll以及如何循环调用正在执行roll的方法。 (请帮助我自己做这个没有老师。)

问题:

编写一个模拟滚动两个骰子的程序。提示用户输入骰子数量。使用循环重复调用模拟骰子滚动的方法,并将两个骰子的总数返回到main。在main中跟踪数组中的卷,并通过显示卷的结果来结束程序。

示例输出:

How many times should I roll the dice? 100
Results for 100 dice rolls 
2 was rolled 4 times
3 was rolled 2 times
4 was rolled 1 times
5 was rolled 8 times
6 was rolled 15 times
7 was rolled 16 times
8 was rolled 17 times
9 was rolled 18 times
10 was rolled 10 times
11 was rolled 6 times
12 was rolled 3 times

到目前为止我的代码:

import java.util.Scanner; 
public class TestingCenter {
    private static Scanner input;   
    public static void main(String[] args){ 

        System.out.println("How many times should I roll the dice? ");
        int answer = input.nextInt();
        for (int x = 0; x < answer; x++) {  


        }
    }


    public static int amount(int x){
            int die1;   
            int die2;   
            int roll;  
            die1 = (int)(Math.random()*6) + 1;
            die2 = (int)(Math.random()*6) + 1;
            roll = die1 + die2;
            return roll;

}
}

4 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情(评论中的解释):

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; 

public class TestingCenter {

    // map to hold the results
    static Map<Integer, Integer> result = new HashMap<>();

    public static void main(String[] args){ 

        // fill the map with numbers from 1 to 12 like the die
        // each number has a corresponding value, initially 0 
        for(int i=1; i<=12; i++){
            result.put(i, 0);
        }

        // initialize the Scanner
        Scanner input = new Scanner(System.in);

        System.out.print("How many times should I roll the dice? ");
        int answer = input.nextInt();

        // repeat the rolling 
        for (int x = 0; x < answer; x++) { 
            rollDice();
        }

        input.close();

        // final result 
        System.out.println("Results for " + answer + " dice rolls:");
        for(Integer die : result.keySet()){
            System.out.println(die + " was rolled " + result.get(die) + " times");
        }
    }

    // this method returns the roll result
    public static void rollDice(){
         int die1, die2, roll;
         die1 = (int)(Math.random()*6) + 1;
         die2 = (int)(Math.random()*6) + 1;
         roll = die1 + die2;
         // increment the current value and update the map result
         result.put(roll, result.get(roll)+1); 
    }
}

<强>测试

How many times should I roll the dice? 100
Results for 100 dice rolls:
1 was rolled 0 times
2 was rolled 5 times
3 was rolled 1 times
4 was rolled 8 times
5 was rolled 17 times
6 was rolled 12 times
7 was rolled 22 times
8 was rolled 8 times
9 was rolled 10 times
10 was rolled 7 times
11 was rolled 9 times
12 was rolled 1 times

答案 1 :(得分:1)

这是int[]方法中main的实施:

import java.util.Random;
import java.util.Scanner;

public class TestingCenter {

private static final Random random = new Random();

public static void main(final String[] args) {

    System.out.println("How many times should I roll the dice? ");
    int answer = 0;
    try (Scanner scanner = new Scanner(System.in)) {
        answer = scanner.nextInt();
    }
    final int[] results = new int[11];
    for (int x = 0; x < answer; x++) {
        results[amount() - 2]++;
    }
    System.out.println(String.format("Results for %s dice rolls ", answer));
    for (int i = 0; i < 11; i++) {
        System.out.println(String.format("%s was rolled %s times", i + 2, results[i]));
    }
}

    public static int amount() {
        return random.nextInt(6) + random.nextInt(6) + 2;

    }
}

测试输出:

How many times should I roll the dice? 
100
Results for 100 dice rolls 
2 was rolled 1 times
3 was rolled 8 times
4 was rolled 10 times
5 was rolled 12 times
6 was rolled 14 times
7 was rolled 18 times
8 was rolled 13 times
9 was rolled 12 times
10 was rolled 6 times
11 was rolled 4 times
12 was rolled 2 times

答案 2 :(得分:0)

import java.util.Arraylist,只需将每个值添加到列表中作为循环answer次。

public static void main(String[] args)
{ 
    input = new Scanner(System.in);
    System.out.println("How many times should I roll the dice? ");
    int answer = input.nextInt();
    List<Integer> list = new ArrayList<Integer>();
    for (int x = 0; x < answer; x++)
    {  
        list.add(amount(x));
    }
}

答案 3 :(得分:0)

你可以做的是创建一个HashMap,其中一个键包含roll的结果,第二个值是该结果的推出次数。它会给出这样的东西:

HashMap<Integer, Integer> results = new HashMap<Integer, Integer>();

for (int x = 0; x < answer; x++) {  
     int roll = amount(x);
     if(results.get(roll) == null) {
          results.put(roll, 1);
     } else {
          results.put(roll, results.get(roll) +1);
     }
}

然后,在打印时,您只需执行for循环(从1到12),然后获取与循环的每个数字相关联的值。