1

I am creating a dice game that has different methods. However, I cannot seem to figure out how to pass variables between my methods for use in the other method. It starts with a menu where the user will "select" either 1 for odd or 2 for even then goes to the roll method to get the sum of the roll and then the calculation method to tell the user whether they have one or loss. However, I cannot seem to figure out how to pass diceSum and select between my methods for use in the calculations method.

I cannot seem to figure out how to get select and diceSum to be used in the diceCalc() to print out the answer for the user.

private static void guess(){
    Scanner input = new Scanner(System.in);
    System.out.print("(1) Odd\n"
            +"(2) Even\n"
            +"(3) Quit Game\n"
            +"What is your selection: ");
    try {
        int select = input.nextInt();
        if(select == 1) {
            System.out.println("");
            diceRoll();
        } else if(select == 2) {
            System.out.println("");
            diceRoll();
        } else if(select == 3) {
            System.out.println("Thank you for playing Dueling Dice v1.0...");
    system.out(0);
}

private static void diceRoll() {
    int roll1, roll2, diceSum;
    roll1 = (int)(Math.random()*6+1);
    roll2 = (int)(Math.random()*6+1);
    diceSum = roll1 + roll2;

    System.out.println("    Dice 1 roll = " + roll1);
    System.out.println("    Dice 2 roll = " + roll2);
    System.out.println("TOTAL dice roll = " + diceSum);
    diceCalc(); // this is called in the main  after the guess since guess performs the roll 
}

private static void diceCalc() {
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) {
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("CONGRATULATIONS! You WIN!!");
        won++;
        played++;
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("I am sorry you lost!");
        lost++;
        played++;
    }
    System.out.print("it is making it here!");
  • How do you print a specific String? You pass it as argument of the println() method, right? So, how do you thing diceCalc should have access to diceSum? Shouldn't it take it as argument, so that diceRoll(), which calls diceCalc(), can pass it as argument? – JB Nizet May 26 '17 at 16:55
  • Did you manage to solve the problem? – luizfzs Jun 22 '17 at 17:02

6 Answers6

1

You can pass diceSum to diceCalc method.

diceCalc(diceSum);  // this is called in the main  after the guess since guess performs the roll 

then add the parameters to diceCalc function.

private static void diceCalc(int diceSum)
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

You could use some read on functions and parameters in Java. Check here.

Despite of that, you can do like this:

Pass select parameter to diceRoll method

private static void guess(){
  int select = <someValue>
  ...
  diceRoll(select);
  ...
}

On the diceRoll method, you declare the parameter like below.

private static void diceRoll(int varSelect) {
  ...
  System.out.println("Select value: " + varSelect);
  ...
}

Edit 1: Just keep in mind that primitive types in Java are passed by value, meaning that if you change the value of varSelect inside diceRoll, it will not update the value of select on the scope of the guess method. Check this question: Is Java “pass-by-reference” or “pass-by-value”?

luizfzs
  • 1,328
  • 2
  • 18
  • 34
0

Here is example:

void methodA (){
int toB = 5;
methodB(toB);
}
void methodB(int fromA){
 int recievedFromA = fromA;
System.out.println(recievedFromA);
}

What book or course are you using? I wonder that you come to writing some console game before learning most basic concepts of programming. If you are self teaching yourself then I suggest you to find some good java book like Effective Java by Joshua Bloch to help you with most basic and advanced concepts that you need to learn.

FilipRistic
  • 2,661
  • 4
  • 22
  • 31
0

Variables declared within methods (for example diceSum) are only local to that specific method. Other methods cannot access them.

public static void main(String[] args) {
    int diceSum; // Can only be used in this method
}

public static void diceCalc() {
    // The variable diceSum is not visible here, it can only be accessed
    // by the main() method
}

However, you can pass parameters to methods. You just put them between the parenthesis:

public static void main(String[] args) {
    int diceSum = 3;
    diceCalc(diceSum);
}

public static void diceCalc(int diceSum) {
    // diceSum will contain the value 3
    ...
}

It's a good thing to make things more dynamic. For instance, the diceCalc() method depends on the menu item number of whether it's odd or even. If you ever want to change the menu-items (or build a graphical user interface where the user clicks a button to choose between odd or even), you must also change the diceCalc() method.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

Thank you for the help but figured it out. I knew I had to change my method to an int to return an argument and it worked perfectly.

    private static int diceRoll() {
    int roll1, roll2, diceSum;
    roll1 = (int)(Math.random()*6+1);
    roll2 = (int)(Math.random()*6+1);
    diceSum = roll1 + roll2;

    System.out.println("    Dice 1 roll = " + roll1);
    System.out.println("    Dice 2 roll = " + roll2);
    System.out.println("TOTAL dice roll = " + diceSum);
    return diceSum;

}

private static void diceCalc() {
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) {
        if (select == 1) {
            System.out.println("\nDice rolled = " + diceSum + " and you selected odd");
        } else {
            System.out.println("\nDice rolled = " + diceSum + " and you selected even");
        }
        System.out.println("CONGRATULATIONS! You WIN!!");
        won++;
        played++;
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("I am sorry you lost!");
        lost++;
        played++;
    }
}
0

It's better in your case to declare class Dice then define your methods as members of Dice

Also define select and diceSum as member variables of class Dice

After that it will be simple to access these variables within any member methods of class Dice

In this way your program will be better organized and easy to read.

public class Dice {

    private int select;
    private int diceSum;

    private void guess(){
    Scanner input = new Scanner(System.in);
    System.out.print("(1) Odd\n"
            +"(2) Even\n"
            +"(3) Quit Game\n"
            +"What is your selection: ");

        select = input.nextInt();
        if(select == 1) {
            System.out.println("");
            diceRoll();
        } else if(select == 2) {
            System.out.println("");
            diceRoll();
        } else if(select == 3) {
            System.out.println("Thank you for playing Dueling Dice v1.0...");
        }
   }
    private void diceRoll() {
    int roll1, roll2;
    roll1 = (int)(Math.random()*6+1);
    roll2 = (int)(Math.random()*6+1);
    diceSum = roll1 + roll2;

    System.out.println("    Dice 1 roll = " + roll1);
    System.out.println("    Dice 2 roll = " + roll2);
    System.out.println("TOTAL dice roll = " + diceSum);
    diceCalc(); // this is called in the main  after the guess since guess performs the roll 
    }

    private void diceCalc() {
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) {
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("CONGRATULATIONS! You WIN!!");
        won++;
        played++;
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("I am sorry you lost!");
        lost++;
        played++;
    }
    System.out.print("it is making it here!");
    }
}
Oghli
  • 2,200
  • 1
  • 15
  • 37