java - writing an application but the numbers wont add up, help please -
i restating question unclear.
i writing dice rolling application randomly rolls dice, displays them, , prompts user again. programme composed of 4 classes, die class creates die objects, pairofdice class knows rolling 2 dice, , validator class validated user input avoid exceptions. have written of code , struggling puzzling problem.
the programme supposed recognize values of dice roll. example, if user rolls 1 , 1 should snake eyes!, when rolling double 6's says box cars! etc. accomplish have added code adds values of both die rolls , determines whether or not fall category.
so question follows: when run application, works fine aside fact numbers displays , sum gets both dice not match up, see roll of (for example) 2 , 4 , see actual total of 10 ( added command statement display the actual total)
so numbers not match , im not sure why...
any advice appreciated!
public class dicerollerapp { public static void main(string[] args) { system.out.println("welcome paradise roller application!"); system.out.println("good luck!"); system.out.println(); scanner sc = new scanner(system.in); int rollcounter; rollcounter = 0; pairofdice dice; dice = new pairofdice(); string selection = validator.getstring(sc, "roll dice? (y/n): "); while (choice.equalsignorecase("y")) { diceroller.rolldice(); rollcounter++; //the counter incremented system.out.println("roll " + rollcounter + ":"); system.out.println(dice.getvalue1()); system.out.println(dice.getvalue2()); system.out.println(); dice.roll(); selection = validator.getstring(sc, "roll again? (y/n): "); system.out.println(); } system.out.println(); } } class die //this problem, how utilize class pairofdice class pairofdice class { private int sides; private int value; public die() { this.sides = 6; } // default six-sided die public die(int sides) { this.sides = sides; } // variable number of sides public void roll() { value = (int)(math.random() * (sides-1)) + 1; } // randomly picks face value public int getvalue() { homecoming value; } } class pairofdice //i think might culprit { private int die1; // instance variable number showing on first die. private int die2; // instance variable number showing on sec die. private int sum; // instance variable hold sum of 2 dice public pairofdice() // default six-sided dice { roll(); // calls roll() method roll dice. } public void roll() // right?? { die1 = (int)(math.random()*6) + 1; die2 = (int)(math.random()*6) + 1; } public int getvalue1() // homecoming number showing on first die. { homecoming die1; } public int getvalue2() // homecoming number showing on sec die. { homecoming die2; } public int getsum() // homecoming total showing on 2 dice. { sum = die1 + die2; homecoming sum; } } class validator { public static string getstring(scanner sc, string prompt) { system.out.print(prompt); string s = sc.next(); // read user entry sc.nextline(); // discard other info entered on line homecoming s; } public static int getint(scanner sc, string prompt) { int = 0; boolean isvalid = false; while (isvalid == false) { system.out.print(prompt); if (sc.hasnextint()) { = sc.nextint(); isvalid = true; } else { system.out.println("error! invalid integer value. seek again."); } sc.nextline(); // discard other info entered on line } homecoming i; } public static int getint(scanner sc, string prompt, int min, int max) { int = 0; boolean isvalid = false; while (isvalid == false) { = getint(sc, prompt); if (i <= min) { system.out.println( "error! number must greater " + min + "."); } else if (i >= max) { system.out.println( "error! number must less " + max + "."); } else { isvalid = true; } } homecoming i; } public static double getdouble(scanner sc, string prompt) { double d = 0; boolean isvalid = false; while (isvalid == false) { system.out.print(prompt); if (sc.hasnextdouble()) { d = sc.nextdouble(); isvalid = true; } else { system.out.println("error! invalid decimal value. seek again."); } sc.nextline(); // discard other info entered on line } homecoming d; } public static double getdouble(scanner sc, string prompt, double min, double max) { double d = 0; boolean isvalid = false; while (isvalid == false) { d = getdouble(sc, prompt); if (d <= min) { system.out.println( "error! number must greater " + min + "."); } else if (d >= max) { system.out.println( "error! number must less " + max + "."); } else { isvalid = true; } } homecoming d; } } class diceroller { // class responsible rolling dice public static void rolldice() { pairofdice dice; dice = new pairofdice(); dice.roll(); system.out.println(dice.getsum()); //control statement see actual sum if (dice.getsum() == 7) { system.out.println("craps!" + "\n"); } else if (dice.getsum() == 2) { system.out.println("snake eyes!" + "\n"); } else if (dice.getsum() == 12) { system.out.println("box cars!" + "\n"); } } }
i think tracked downwards bug
in main
, alter
diceroller.rolldice();
to
diceroller.rolldice(dice);
and alter rolldice
public static void rolldice() { pairofdice dice; dice = new pairofdice(); dice.roll(); system.out.println(dice.getsum()); //control statement see actual sum if (dice.getsum() == 7) { system.out.println("craps!" + "\n"); } else if (dice.getsum() == 2) { system.out.println("snake eyes!" + "\n"); } else if (dice.getsum() == 12) { system.out.println("box cars!" + "\n"); } }
to
public static void rolldice(pairofdice dice) { // pairofdice dice; // dice = new pairofdice(); dice.roll(); system.out.println(dice.getsum()); //control statement see actual sum if (dice.getsum() == 7) { system.out.println("craps!" + "\n"); } else if (dice.getsum() == 2) { system.out.println("snake eyes!" + "\n"); } else if (dice.getsum() == 12) { system.out.println("box cars!" + "\n"); } }
explanation
you create new pairofdice
object each time, printed sum object, printed 2 outcomes other object defined in `main. these changes. you utilize 1 object, , results consistent.
output
welcome paradise roller application! luck! roll dice? (y/n): y 4 roll 1: 3 1 roll again? (y/n): y 6 roll 2: 5 1 roll again? (y/n): y 4 roll 3: 1 3 roll again? (y/n):
java
No comments:
Post a Comment