Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll be honest, this is a total mess (but we were all new at one time, right)? What you really need to do is just go through the tutorials on writing Java apps, and especially object-oriented concepts, and learn as you go. The fundamental object-oriented concept, that methods represent actions/abilities of an object they're attached to, as opposed to being a list of mostly standalone functions/subroutines, will really help you see what's different about OO-programming vs. functional programming.</p> <p>I'll go section-by-section and try to help you out here.</p> <p><strong>Section 1:</strong></p> <pre><code>// MAIN METHOD public static void main(String[] args) { // SET VARIABLES FOR DIE HIGH AND LOW VALUES, NUMBER OF DICE TO ROLL final int LOWEST_DIE_VALUE = 1; final int HIGHEST_DIE_VALUE = 6; final int DICE_TO_ROLL = 5; // ROLL A SINGLE DIE VIA METHOD rollADie() int roll = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE); System.out.println("roll " + roll); } </code></pre> <p>Ok, so you've got a main method. Good start. All Java apps need this. However:</p> <ul> <li>You're declaring final variables in the main method, which isn't really necessary. Remove the <code>final</code> key words here, re-capitalize the variable names to the <code>camelCase</code> convention, and just pass them in as normal variables.</li> <li>Also, and this is just a style point, you're declaring <code>lowestDieValue</code> followed by <code>highestDieValue</code>, which makes total sense, but then you're passing them in highest first, then lowest. This feels a little weird and inconsistent, so I've flipped them around.</li> <li>Finally, there's no reason to do your comments in ALL CAPS either, so as another style point, I would just use normal case. In your code, however, I think your variable and method names are descriptive enough; you really don't need the comments at all, so I've taken them out.</li> </ul> <p>So section 1, after these changes, should look something like:</p> <pre><code>public static void main(String[] args) { int lowestDieValue = 1; int highestDieValue = 6; int diceToRoll = 5; int roll = rollADie(lowestDieValue, highestDieValue); System.out.println("roll " + roll); } </code></pre> <p><strong>Section 2:</strong></p> <ul> <li>Re-order the variable names as noted above, and don't use ALL CAPS - by convention ALL CAPS are reserved for constants (i.e. <code>final</code> variables). When you pass it into a method, they're not finals/constants.</li> <li>This method is overly verbose. It does exactly one useful thing, so it can be written in just a single line.</li> </ul> <p>When you make these changes, section 2 should look like:</p> <pre><code>public static int rollADie(int lowestDieValue, int highestDieValue) { return ((int)(Math.random()*100)%highestDieValue+lowestDieValue); } </code></pre> <p><strong>Section 3:</strong></p> <p>This method can be greatly simplified. We're basically trying to accomplish three things:</p> <ol> <li>Create an array in which to store the rolled die values</li> <li>Loop through rolling dice to get the values, storing each created value in the array</li> <li>Return the resulting <code>int[]</code> (integer array)</li> </ol> <p>You've got several problems in this method, including calling the <code>return</code> statement in the middle of a <code>for</code> loop (which is compilable, but is technically incorrect in your case as it results in your method returning early with only one value in the array). </p> <p>I'm just going to completely re-write this method, including renaming/re-capitalizing your variable names according to the aforementioned conventions. The rewrite cleanly accomplishes the 3 things this method needs to do (as specified above):</p> <pre><code>public static int[] rollTheDice(int numDiceToRoll, int lowestDieValue, int highestDieValue) { int[] allDiceRolls = new int[numDiceToRoll]; for(int i = 0; i &lt; allDiceRolls.length; i++) { allDiceRolls[i] = rollADie(lowestDieValue, highestDieValue); } return allDiceRolls; } </code></pre> <p>Finally:</p> <ul> <li><strong>Style:</strong> it's always been my habit to put opening <code>{</code> at the end of a line, as opposed to on their own line, but that's just a style thing. Either style is valid and means the same thing when compiled.</li> <li><strong>Code:</strong> Your class is called "FiveDice" but you don't actually roll 5 dice in your main method, nor do you ever call the <code>rollTheDice</code> method.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload