0

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

}

}

4

3 回答 3

2

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

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

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

于 2013-10-10T02:29:56.840 回答
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),那我就交给您了。这是非常微不足道的。

于 2013-10-10T04:03:10.357 回答
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
    }
}```
于 2020-05-18T06:20:53.683 回答