943,661 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2271
  • Java RSS
Mar 3rd, 2008
0

Beginner help w. Java - loops etc

Expand Post »
I have been helped here once before. I got the info I needed to solve my issues. I'm a beginner and this online class I'm taking is killing me. This assignment involves the use of loops, most likely do-while, and possibly others. Essentially the program is to have the user guess what the total of his roll of two dice will be (2 to 12). Then the two dice are rolled. The user has three rolls to achieve a WIN, if not he loses and must get the opportunity to play again. If he matches his guess, the game tells him he's a winner and the game is over then - no three rolls. He also must be given the opportunity to play again.
I've validated the user's guess (input). That part works great. This program uses a class that rolls and displays the dice - that too works ok. From there, I've gotten completely confused and frustrated. Here is my code, most of which does not work. Can anyone help me? I'm such a beginner, the book doesn't always help, and there is no online course assistance. Thanks in advance for any hints or help:
Java Syntax (Toggle Plain Text)
  1. //********************************************************************
  2. // DiceGame.java
  3. // This program tells user to predict what the pair of dice will roll.
  4. // Dice will roll a maximum of 3 times, using the Dice class already created.
  5. // If the dice rolls the user's prediction in the 3 rolls, user wins
  6. // If not, user loses. Either way user has option to play again.
  7. //********************************************************************
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class DiceGame
  12. {
  13. public static void main (String[] args)
  14. {
  15. // All variables including char variable that holds y and n.
  16. int rolls, roll1, roll2, guessRoll;
  17. int rollSum;
  18. char repeat = ' ';
  19. String input;
  20. Scanner keyboard = new Scanner(System.in);
  21.  
  22.  
  23. // Create two separate dice objects to create a pair of dice
  24. Dice die1 = new Dice();
  25. Dice die2 = new Dice();
  26.  
  27. // get the user to enter the what user predicts his/her pair of dice will roll
  28. System.out.print ("Enter what your pair of dice will roll (a number 2 through twelve please: ");
  29. guessRoll = keyboard.nextInt();
  30. // Validate user's input - page 261
  31. while (guessRoll < 2 || guessRoll > 12)
  32. {
  33. System.out.println("Incorrect number entered. Please enter a number from 2 to to 12 only.");
  34. System.out.println("Enter correct number here: ");
  35. guessRoll = keyboard.nextInt();
  36. }
  37. {
  38. do
  39. {
  40. roll1 = die1.diceRoll();
  41. roll2 = die2.diceRoll();
  42.  
  43. rollSum = roll1 + roll2;
  44. // Set the number of times the loop can repeat itself.
  45. for (rolls = 1; rolls < 3; rolls++)
  46. {
  47. System.out.println("Your Dice rolled: " + rollSum +".");
  48. System.out.println("NO MATCH");
  49. }
  50. while (repeat == 'Y' || repeat == 'y')
  51. { System.out.println("Play again? Enter Y or N: ");
  52. input = keyboard.next();
  53. repeat = input.charAt(0);
  54. }
  55. } while (guessRoll != rollSum);
  56. }
  57. do
  58. {
  59. //This will run if the correct number is guessed
  60. System.out.println("_______________Roll #: " + rolls + " _________________");
  61. roll1 = die1.diceRoll();
  62. roll2 = die2.diceRoll();
  63. rollSum = roll1 + roll2;
  64. System.out.println("Your Dice rolled: " + rollSum +".");
  65. System.out.println("IT'S a MATCH - YOU WIN!!");
  66. while (repeat == 'Y' || repeat == 'y')
  67. {
  68. System.out.println("Play again? Enter Y or N: ");
  69. input = keyboard.next();
  70. repeat = input.charAt(0);
  71. }
  72. } while (guessRoll == rollSum);
  73.  
  74. }
  75.  
  76. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brinze is offline Offline
9 posts
since Feb 2008
Mar 3rd, 2008
0

Re: Beginner help w. Java - loops etc

here is an example of how you ask the user if he wants to play again
[code]
ยจ.....


boolean playAgain; // Record user's response when user is
// asked whether he wants to play
// another game.

do {
int scoreThisGame; // Score for one game.
scoreThisGame = play(); // Play the game and get the score.
sumOfScores += scoreThisGame;
gamesPlayed++;
TextIO.put("Play again? ");
playAgain = TextIO.getlnBoolean();
} while (playAgain);

[\code]

for more information , check this web http://math.hws.edu/eck/cs124/javanotes3/c5/s3.html

let me know if this helps good luck
Reputation Points: 10
Solved Threads: 0
Newbie Poster
peaceful_soul is offline Offline
12 posts
since Feb 2008
Mar 3rd, 2008
0

Re: Beginner help w. Java - loops etc

Thanks for the assist.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brinze is offline Offline
9 posts
since Feb 2008
Mar 3rd, 2008
0

Re: Beginner help w. Java - loops etc

no problem , anytime
Reputation Points: 10
Solved Threads: 0
Newbie Poster
peaceful_soul is offline Offline
12 posts
since Feb 2008
Mar 4th, 2008
0

Re: Beginner help w. Java - loops etc

Thank you so much again. I got the program turned in by its deadline. The "play again" worked great - most of the program did right, except I never did get the loop to do right as far as rolling the dice a max of three times if there was no match. The assignment is turned in AS -IS, so I'm not so desperate anymore, but I still want to know how to do it. I'm at work at the moment, but I'd like to still post my code later - when I am able - to see what I'm doing wrong. I appreciate the help and generosity of your time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brinze is offline Offline
9 posts
since Feb 2008
Mar 4th, 2008
0

Re: Beginner help w. Java - loops etc

I didn't really read your code, and I am a beginner with Java as well, but have done similar things in VB and in flash, as well as used similar concepts in javascript.

So, I can give you the theory here but can not give you exact java code.

Step one;
when the game loads you need to set a variable "myRolls" (give it your own name if you want) equal to 1 (or zero if you wish).
After each roll, give it a new number of
myRolls + 1 (or myRolls ++ as would work in most languages).

At each interval, you need to do the following.
Check if player's guess matched, if not then check the value of myRolls. If myRolls == 3... If so, game over, if not, roll again.

If the player has guessed, no need to do anything else except "you win, wanna play again"...
Make sure that if they choose to play again, you set myRolls back to the default number again.
Hope this helps.
Sage
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
sagedavis is offline Offline
86 posts
since Nov 2007
Mar 4th, 2008
0

Re: Beginner help w. Java - loops etc

sure brinze , anytime
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
wonder_laptop is offline Offline
89 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: post method [problem with code]
Next Thread in Java Forum Timeline: JSP input fields and tabpage





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC