随机骰子发生器

时间:2013-10-10 02:18:50

标签: java random compare

运行是一系列相邻的重复值。编写一个程序,生成一系列随机模具抛出并打印模具值,仅标记最长的运行。程序应该将掷骰子总数(例如10)作为输入,然后打印:

1 6 6 3(2 2 2 2 2)5 2

我很困惑如何比较每个数字以获得正确的输出。也许使用数组来存储值。任何答案或意见都会有所帮助,谢谢!

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

 public class Dice 
 {
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);

public void DiceCount()
{
int count;
int sides = 6;
int number;
System.out.println("How many die? ");
count = keyboard.nextInt();
for(int i=0; i < count; i++)
{
    number =  generator.nextInt(sides);
    System.out.print(number);
}

}

}

3 个答案:

答案 0 :(得分:2)

首先,将int number;替换为int[] numbers = new int[count];。接下来,将number = ...替换为numbers[i] = ...

这会给你一组随机数(不要打印它们!)。在生成数字时,请注意连续数量相等的数字(为此添加一个特殊计数器)。还要添加存储目前为止最长运行长度的变量。每当你得到一个等于前一个数字的数字时,递增计数器;否则,将计数器与最大值进行比较,必要时更改最大值,并将计数器设置为1。更新最大值时,请标记运行开始的位置(可以从当前位置和运行长度判断)。

现在是时候检测最长的运行:遍历numbers数组,并在运行开始时放置一个左括号。到达运行结束时放置右括号,并完成打印以完成分配的输出。

答案 1 :(得分:0)

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

public class Dice {
    Random generator = new Random();
    Scanner keyboard = new Scanner(System.in);

    public void DiceCount() {
        int sides = 6;
        System.out.println("How many die? ");
        int count = keyboard.nextInt();
        int[] array = new int[count];
        int longestLength = 1, currentLength = 1, longestLengthIndex = 0, currentLengthIndex = 1;
        int currentNum = -1;
        for (int i = 0; i < count; i++) {
            array[i] = generator.nextInt(sides);
            System.out.print(array[i] + " ");
            if (currentNum == array[i]) {
                currentLength++;
                if (currentLength > longestLength) {
                    longestLengthIndex = currentLengthIndex;
                    longestLength = currentLength;
                }
            } else {
                currentLength = 1;
                currentLengthIndex = i;
            }
            currentNum = array[i];
        }
        System.out.println();
        for (int i = 0; i < count; i++)
            System.out.print((i == longestLengthIndex ? "(" : "") + array[i] + (i == (longestLengthIndex + longestLength - 1) ? ") " : " "));
    }
}

注意:这只是第一个最长的范围。所以如果你有1123335666它会做112(333)5666。 如果你需要112(333)5(666)或1123335(666),那么我把它留给你。这非常简单。

答案 2 :(得分:0)

import java.util.Random;

public class Dice {
    public static void main(String[] args) {
        //make rolls
        Random rand = new Random();
        int[] array = new int[20];
        int longestRun = 1;
        int currentRun = 1;
        int longestRunStart = 0;
        int currentRunStart = 1;

        System.out.print("Generated array: \n");
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextInt(6); //add random number
            System.out.print(array[i] + " "); //print array
            if (i != 0 && array[i - 1] == array[i]) {
                //if new number equals last number...
                currentRun++; //record current run
                if (currentRun > longestRun) {
                    longestRunStart = currentRunStart; //set index to newest run
                    longestRun = currentRun; //set above record to current run
                }
            } else {
                //if new number is different from the last number...
                currentRun = 1; //reset the current run length
                currentRunStart = i; //reset the current run start index
            }
        }

        //record results
        System.out.print("\nIdentifying longest run: \n");
        for (int i = 0; i < longestRunStart; i++) { System.out.print(array[i] + " "); } //prints all numbers leading up to the run
        System.out.print("( "); //start parentheses
        for (int i = longestRunStart; i < (longestRunStart + longestRun); i++) { System.out.print(array[i] + " "); } //prints the run itself
        System.out.print(") "); //end parentheses
        for (int i = (longestRunStart + longestRun); i < 20; i++) { System.out.print(array[i] + " "); } //all remaining numbers
    }
}```