首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机骰子发生器

随机骰子发生器
EN

Stack Overflow用户
提问于 2013-10-10 02:18:50
回答 3查看 4.6K关注 0票数 0

run是一个相邻重复值的序列。编写一个程序,生成随机掷模序列,并打印模具值,只标记最长的运行时间。程序应以掷模总数(例如10次)为输入,然后打印:

6 6 3 (2 2 2) 5 2

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

代码语言:javascript
复制
 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);
}

}

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-10 04:03:10

代码语言:javascript
复制
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,它会做112335666。如果你需要112(333)5(666)或1123335(666),那我就留给你。这是非常微不足道的。

票数 0
EN

Stack Overflow用户

发布于 2013-10-10 02:29:56

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

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

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

票数 2
EN

Stack Overflow用户

发布于 2020-05-18 06:20:53

代码语言:javascript
复制
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
    }
}```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19285953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档