-1

I had help writing this program as a simulation of a dice game, but I can't seem to get it to run because of the syntax/bracket situation. I think I am having trouble organizing my methods within the classes. Any tips?

import java.util.Random;

public class diceGame 
{
    private class DiceRoll
    {
        public static Random rng = new Random();
        private int a, b, c; //rolls

        public DiceRoll()
        {
            this.a = rng.nextInt(6);
            this.b = rng.nextInt(6);
            this.c = rng.nextInt(6);
        }

        public int getScore()
        {
            if (a ==6 && b == 6 && c ==6)
            {
                return 500;
                System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
            }
            else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c !=a))
            {
                return 100;
                System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
            }
            else if ((a == b && b != c) || (b == c && c !=a))
            {
                return 50;
                System.out.println("Congratulations! You just earned 50 points!!");
            }
            else 
            {
                System.out.println("Nice Try!");
            }
        }

        public String toString()
        {
            return String.format("Rolled a %d, %d, and %d", a, b, c);
        }
    }

    public static void main (String args[])
    {
        DiceRoll d; 
        while (true)
        {
            d = new DiceRoll();
            d.getScore();        // gives you how much to change the player score
        }
        // finish client
    }
}
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325

3 Answers3

2

One look:

if (a ==6 && b == 6 && c ==6){
  return 500;
  System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); //This is the error!
}

Once you return from a method, any other code will not be run and it will cause a compile error. I think this is what you want:

if (a ==6 && b == 6 && c ==6) {
  System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
  return 500;
}

Also, look at this:

public static void main(String args[]) {
  DiceRoll d;
  while (true) {
    d = new DiceRoll();
    d.getScore(); // gives you how much to change the player score
  } // finish client
}
}
} //Extra

Remove the curly brace marked "Extra".

Also, the Random rng is static, but it is declared in a non-static class.

Change it to private static class DiceRoll

Your getScore() method is borked; It does not return anything.

    public int getScore() {
        if (a == 6 && b == 6 && c == 6) {
            return 500;
            System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
        } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
            return 100;
            System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
        } else if ((a == b && b != c) || (b == c && c != a)) {
            return 50;
            System.out.println("Congratulations! You just earned 50 points!!");
        } else {
            System.out.println("Nice Try!");
            //Wut no return?
        }
    }

You might want:

  public int getScore() {
    if (a == 6 && b == 6 && c == 6) {
      System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
      return 500;
    } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
      System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
      return 100;
    } else if ((a == b && b != c) || (b == c && c != a)) {
      System.out.println("Congratulations! You just earned 50 points!!");
      return 50;
    } else {
      System.out.println("Nice Try!");
      return -1;
    }
  }

And its a good idea to call your diceGame class DiceGame

Universal Electricity
  • 775
  • 1
  • 12
  • 26
1

You have multiple problems in your code

  1. There are no return statements for some methods
  2. There are few Sysout's after return (which makes the code unreachable)
  3. The concept of Parent class Inner class is wrong. In an inner class , speaking of static, you can have only constants which are both static and final.
  4. The bracketing is wrong.

The below code would work and only terminate when all the dices are rolled 6.

import java.util.Random;

public class diceGame {
    private class DiceRoll {
        public Random rng = new Random();
        private int a, b, c; // rolls

        public DiceRoll() {
            this.a = rng.nextInt(6);
            this.b = rng.nextInt(6);
            this.c = rng.nextInt(6);
        }

        public int getScore() {
            if (a == 6 && b == 6 && c == 6) {
                System.out
                        .println("Congratulations! You rolled three 6s! You just earned 500 points!!");
                return 500;

            } else if ((a == 6 && b == 6 && b != c)
                    || (b == 6 && c == 6 && c != a)) {
                System.out
                        .println("Congratulations! You rolled two 6s! You just earned 100 points!!");
                return 100;

            } else if ((a == b && b != c) || (b == c && c != a)) {
                System.out
                        .println("Congratulations! You just earned 50 points!!");
                return 50;

            } else {
                System.out.println("Nice Try!");
                return 0;
            }
        }

        public String toString() {
            return String.format("Rolled a %d, %d, and %d", a, b, c);
        }
    }

    public static void main(String args[]) {
        DiceRoll d;
        while (true) {
            d = new diceGame().new DiceRoll();
            if (d.getScore() == 500) { // gives you how much to change the player score

                break;
            }

        }
        // finish client
    }
}

You would need to fix all these for the dice to roll.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Kalyan Chavali
  • 1,330
  • 8
  • 24
1

There is quite few problems with your code. The first one is idea about using inner class, so that inner class should go out. All methods can go to outer class. Placing rolling into constructor makes game boring, a, b and c will stay the same for lifetime of instance or forces you to create new instance for each roll. Really bad design decision. I would move that into getScore. Finally while(true) will go on forever and you will have to kill program to stop it. Here is your code and check scoring logic, I do not know rules:

import java.util.Random;

public class diceGame {
  public static Random rng = new Random();
  private int a, b, c; //rolls
  public int getScore() {
    this.a = 1 + rng.nextInt(5);
    this.b = 1 + rng.nextInt(5);
    this.c = 1 + rng.nextInt(5);
    if (a == 6 && b == 6 && c == 6) {
      System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
      return 500;
    } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
      System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
      return 100;
    } else if ((a == b && b != c) || (b == c && c != a) || (a == c && b != a)) {
      System.out.println("Congratulations! You just earned 50 points!!");
      return 50;

    } else {
      System.out.println("Nice Try!");
      return 0;
    }
  }
  public String toString() {
    return String.format("Rolled a %d, %d, and %d", a, b, c);
  }
  public static void main(String args[]) {
    diceGame d = new diceGame();
    for (int i = 0; i < 3; i++) {
      System.out.println(d.getScore());
      System.out.println(d.toString());
    }
  }
}
Filip Bulovic
  • 1,776
  • 15
  • 10