java - Unable to catch the random number generated in main method? -
i running code, unable access class variables (pg , dg) in main method. output getting is
dealer starting game
has guessed number 0 9
time guess
player guessing
number player 1 guessed 0
player guessing
number player 2 guessed 0
player guessing
number player 3 guessed 0
dealer guess 0
have guessed correctly
i unable catch random number generated in method.
class player { public int pg = 0; public void pguess() { system.out.println("player guessing"); int pg = (int)(math.random() * 10); } } class dealer { public int dg = 0; public void guess() { system.out.println("dealer starting game"); system.out.println("he has guessed number 0 9"); system.out.println("now time guess"); int dg = (int)(math.random() * 10); } public void dealerdisplay() { system.out.println("the dealer guess " + dg); } } public class gameon { public static void main(string args[]) { dealer d = new dealer(); player = new player(); player b = new player(); player c = new player(); d.guess(); a.pguess(); system.out.println("the number player 1 guessed " + a.pg); b.pguess(); system.out.println("the number player 2 guessed " + b.pg); c.pguess(); system.out.println("the number player 3 guessed " + c.pg); d.dealerdisplay(); if ((d.dg == a.pg) && (d.dg == b.pg) && (d.dg == c.pg)) system.out.println("all have guessed correctly"); else if ((d.dg == a.pg) && (d.dg == b.pg) || (d.dg == a.pg) && (d.dg == c.pg) || (d.dg == b.pg) && (d.dg == c.pg)) system.out.println("two have have guessed correctly"); else if ((d.dg == a.pg) || (d.dg == b.pg) || (d.dg == c.pg)) system.out.println("one has guessed correctly"); else system.out.println("no 1 has guessed correctly"); } }
here's problem:
system.out.println("player guessing"); int pg = (int)(math.random()*10); }
you're declaring new local int pg instead of setting class member. change to:
system.out.println("player guessing"); pg = (int)(math.random()*10); }
and see if works.
Comments
Post a Comment