随机骰子生成器



运行是相邻重复值的序列。编写一个程序,生成一系列随机抛骰子并打印骰子值,仅标记最长的运行时间。程序应将抛模总数(例如 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);
}
}

}

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

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

现在是时候检测最长的运行了:遍历numbers数组,并在运行开始的地方加上一个左括号。在运行结束时加上右括号,然后完成打印以完成作业的输出。

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(,那么我把它留给你。这是非常微不足道的。

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
    }
}```

相关内容

  • 没有找到相关文章

最新更新