Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm having trouble grasping how to pass variables to and from methods
    primarykey
    data
    text
    <p>I come from a procedural programming background, and need some help grasping how methods pass variables back and forth. The methods work with explicitly declared values, but when I try to pass values from one method to another, I get all kinds of "error: cannot find symbol" messages. </p> <p>I suspect that I'm either a.) declaring the variables in the wrong place and they clear out at the end of the method or b.) how I'm coding the send/return of variables is wrong, or c.) both. More importantly, while I've read my textbook and a number of online tutorial resources, I'm still having trouble grasping how the syntax is supposed to work. Can someone clue me in?</p> <p>The program: In the code provided, I'm trying to roll five six-sided dice. One method rolls a single die, the other calls that method multuiple times and writes the values to the array...I think. </p> <p>Thanks in advance, d </p> <pre><code>public class FiveDice { // 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); } // // RETURNS THE RESULT OF A SINGLE DIE ROLL public static int rollADie(int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE) { int roll; roll = ((int)(Math.random()*100)%HIGHEST_DIE_VALUE+LOWEST_DIE_VALUE); return roll; } // // CALL rollADie TO ROLL DICE_TO_ROLL (above) DICE; RETURN ARRAY OF ROLLED DICE public static int[] rollTheDice(int DICE_TO_ROLL, int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE) { int rollNum; int rolledDie; for(rollNum=1;rollNum&lt;=DICE_TO_ROLL;rollNum++) { int[] rolledDie = new rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE) { rolledDie }; return rolledDie[]; } } } </code></pre>
    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.
 

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