java - Organizing methods within classes in a simulation of a dice game -


i had writing program simulation of dice game, can't seem run because of syntax/bracket situation. think having trouble organizing methods within classes. 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! rolled 3 6s! earned 500 points!!");             }             else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c !=a))             {                 return 100;                 system.out.println("congratulations! rolled 2 6s! earned 100 points!!");             }             else if ((a == b && b != c) || (b == c && c !=a))             {                 return 50;                 system.out.println("congratulations! earned 50 points!!");             }             else              {                 system.out.println("nice try!");             }         }          public string tostring()         {             return string.format("rolled %d, %d, , %d", a, b, c);         }     }      public static void main (string args[])     {         diceroll d;          while (true)         {             d = new diceroll();             d.getscore();        // gives how change player score         }         // finish client     } } } 

one look:

if (a ==6 && b == 6 && c ==6){   return 500;   system.out.println("congratulations! rolled 3 6s! earned 500 points!!"); //this error! } 

once return method, other code not run , cause compile error. think want:

if (a ==6 && b == 6 && c ==6) {   system.out.println("congratulations! rolled 3 6s! earned 500 points!!");   return 500; } 

also, @ this:

public static void main(string args[]) {   diceroll d;   while (true) {     d = new diceroll();     d.getscore(); // gives how change player score   } // finish client } } } //extra 

remove curly brace marked "extra".

also, random rng static, declared in non-static class.

change private static class diceroll

your getscore() method borked; not return anything.

    public int getscore() {         if (a == 6 && b == 6 && c == 6) {             return 500;             system.out.println("congratulations! rolled 3 6s! earned 500 points!!");         } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {             return 100;             system.out.println("congratulations! rolled 2 6s! earned 100 points!!");         } else if ((a == b && b != c) || (b == c && c != a)) {             return 50;             system.out.println("congratulations! 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! rolled 3 6s! earned 500 points!!");       return 500;     } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {       system.out.println("congratulations! rolled 2 6s! earned 100 points!!");       return 100;     } else if ((a == b && b != c) || (b == c && c != a)) {       system.out.println("congratulations! earned 50 points!!");       return 50;     } else {       system.out.println("nice try!");       return -1;     }   } 

and idea call dicegame class dicegame


Comments