基本的Java程序骰子 - 很难找到最长连胜纪录 [英] Basic Java Dice program - trouble finding longest streak

查看:180
本文介绍了基本的Java程序骰子 - 很难找到最长连胜纪录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个基本的(和我所看到的,经常分配),其中我对创建模拟骰子滚动计划的Java项目。在我的项目,我只模拟两个六面骰子的滚动。阵列需要在完成这个任务时使用。

I'm currently working on a basic (and as I can see, frequently assigned) java project in which I'm to create a program that simulates the rolling of dice. In my project I'm only simulating the rolling of two six sided die. Arrays need to be used in completing this task.

到目前为止,我有try / catch块,询问他们想模仿辊的量的用户,并确保他们给一个整数输入。这反过来会创建阵列为骰子模拟器的长度。到现在为止我的code ++工程,但现在我停留在如何在数组中找到连续款项最长的连胜纪录。

So far I have try/catch block that asks the user for the amount of rolls they would like to simulate and ensures that they give an integer input. This in turn creates the length of the array for the dice simulator. Up to this point my code works but now I am stuck on how to find the longest streak of consecutive sums within that array.

我是pretty知道我必须为自己对每卷比较我dieSum值,如果dieSum == dieSum然后通过1增加变量我目前的连胜我只是不确定如何做到这一点,我怀疑我可能有一个问题,因为我的dieSum变量没有被存储到一个数组中。

I'm pretty sure I need to be comparing my dieSum value for each roll against itself, and if dieSum == dieSum then increasing a variable for my current streak by 1. I'm just unsure of how to do this, I suspect I may have a problem because my dieSum variable isn't being stored into an array.

import java.util.Scanner;
public class RollThoseDice {
public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in);

    System.out.println("MyNameHere");
    System.out.println("ThirdProgram \n");

    //Variable listing and initialization
    int rollTotal = 0; //number of trials
    int dieSum; //sum of the outcome of both dice per roll

    //Try - Catch block to ensure positive integer input from user for rollTotal
    System.out.println("***Welcome to the Dice Roll Probability Generator***");
    while (true){
        System.out.println("Enter how many dice rolls you would like to simulate: ");
        try {
            rollTotal = kbd.nextInt() + 1;
            break;
        } catch (Exception e) {
            System.out.println("Please only enter a positive integer. Try again. ");
            kbd.next();

        }
    }


    int die1[] = new int[rollTotal]; //Variable for the first die
    int die2[] = new int[rollTotal]; //Variable for the second die


    int[] diceArray = new int[rollTotal];   
    for (int i=1; i<diceArray.length; i++ ) {
        die1[i] = (int)(6.0*Math.random() + 1.0); //Generate random # between 1-6
        die2[i] = (int)(6.0*Math.random() + 1.0);
        System.out.println("***Roll " + i + "***");
        System.out.print("Dice one rolled: " + die1[i] + ", dice two rolled: " + die2[i] + "\n") ;
        dieSum = die1[i] + die2[i];
        System.out.println("Total rolled was: " + dieSum + "\n");

    }
}

}

非常感谢采取偷看在此我,原谅我的新鲜感一般网站和编程。

Thanks a lot for taking a peek at this for me, and excuse my freshness to the site and programming in general.

推荐答案

您需要保持两个变量的轨迹: INT biggestStreak,currentStreak (初始化为1)。

You need to keep track of two more variables: int biggestStreak, currentStreak (initialized with 1).

在你掷骰子和储存的变量,可以检查你的卷相同的前滚翻,增加 currentStreak

After you rolled the dice and and stored the variables, you can check if your roll is the same as the roll before and increase currentStreak:

int lastDieSum = die1[i-1] + die2[i-1];

if (lastDieSum == dieSum) {
     currentStreak++;
     if (currentStreak > biggestStreak) {
         biggestStreak = currentStreak;
     } 
} else {
     currentStreak = 1;
}

的最后一位现在还存储在 biggestStreak 当前条纹如果是比最大条纹更大。最后,目前的连胜被设置回零,如果值是不一样了,因为在 lastDieSum

The last bit now also stores the current streak in biggestStreak if it is bigger than the biggest streak. Finally, the current streak is set back to zero when the value isn't the same anymore as the lastDieSum.

我希望这有助于!

这篇关于基本的Java程序骰子 - 很难找到最长连胜纪录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆