Results 1 to 7 of 7
- 11-21-2010, 08:58 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
help with program!! looping issue?
hi! i'm new here and really new to programming! i come here with an issue, of course.
please bear with me, my syntax, etc. feel free to evaluate my current errors, since it's not necessarily done. my main issue right now, however, is how to loop it back to the user's turn once the computer is done rolling , particularly after it rolls a 1.. & i'm not particularly sure on where to place another 'if', for the 100 point goal.
solved, thanks to muskar!Last edited by zombian; 11-21-2010 at 10:00 PM.
- 11-21-2010, 09:34 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
This is my shot at it:
Java Code:import java.util.Scanner; public class PigGame{ public static void main (String[] args){ int [COLOR="Red"]userRound[/COLOR] = 0, [COLOR="Green"]userPoints[/COLOR] = 0, [COLOR="Navy"]compPoints[/COLOR] = 0, num1, num2, [COLOR="Plum"]compRound[/COLOR] = 0; Die die1 = new Die(); Die die2 = new Die(); Scanner Keyboard = new Scanner(System.in); boolean turnover = false; boolean play = false; System.out.println("Play Pig? (1 for Yes, 2 for No): "); int input = Keyboard.nextInt(); if (input == 2){ play = false; System.out.print("Good bye."); } else if (input == 1) { play = true; } while (play == true) { //PLAYER'S TURN while (turnover == false) { //Rolls dice num1 = die1.roll(); num2 = die2.roll(); [COLOR="Red"]userRound[/COLOR] = [COLOR="Red"]userRound[/COLOR] + num1 + num2; System.out.println("You rolled: " +num1+ " & " + num2); System.out.println("Points: " + [COLOR="Red"]userRound[/COLOR]); System.out.println(); //Checks for 1's if (num1 == 1 || num2 == 1) { [COLOR="Red"]userRound[/COLOR] = 0; if (num1 == 1 && num2 == 1) { [COLOR="Green"]userPoints[/COLOR] = 0; } turnover = true; System.out.println("Sorry! No 1's allowed."); System.out.println("Points: " + [COLOR="Green"]userPoints[/COLOR]); System.out.println("Computer's turn."); System.out.println(); } [B][COLOR="Lime"]else if ([COLOR="Green"]userPoints[/COLOR] >= 100) { System.out.println("You have " + [COLOR="Navy"]compPoints[/COLOR] + "points!"); System.out.println("You won!"); play = false; } [/COLOR][/B]else { System.out.print("Roll again? (1 for Yes, 2 for No): "); input = Keyboard.nextInt(); if (input == 2) { turnover = true; [COLOR="Green"]userPoints[/COLOR] = [COLOR="Green"]userPoints[/COLOR] + [COLOR="Red"]userRound[/COLOR]; System.out.println("Computer's turn!"); System.out.println(); } } } //COMPUTER'S TURN while (turnover == true) { num1 = die1.roll(); num2 = die2.roll(); [COLOR="Plum"]compRound[/COLOR] = [COLOR="Plum"]compRound[/COLOR] + num1 + num2; System.out.println("Computer rolled: " + num1 + " & " + num2); System.out.println("Points: " + [COLOR="Plum"]compRound[/COLOR]); System.out.println(); //Checks for 1's if (num1 == 1 || num2 == 1) { [COLOR="Plum"]compRound[/COLOR] = 0; turnover = false; if (num1 == 1 && num2 == 1) { [COLOR="Navy"]compPoints[/COLOR] = 0; } [COLOR="Navy"]compPoints[/COLOR] = [COLOR="Navy"]compPoints[/COLOR] + [COLOR="Plum"]compRound[/COLOR]; System.out.println("Computer's points: " + [COLOR="Navy"]compPoints[/COLOR]); System.out.println("Rolled a 1. Back to user."); System.out.println(); turnover = false; } [B][COLOR="Lime"]else if ([COLOR="Navy"]compPoints[/COLOR] >= 100) { System.out.println("Computer has " + [COLOR="Navy"]compPoints[/COLOR] + "points!"); System.out.println("You lost!"); play = false; }[/COLOR][/B] else if ([COLOR="Plum"]compRound[/COLOR] >= 20){ System.out.println("User's turn."); System.out.println(); turnover = false; } } } } }
- I've renamed a few variables (I'll edit this post in a second and use colours so they stand out - done)
- Changed a few syntaxes so they are easier to read in my opinion
- Changed your "while input == 1" to "while play == true" (could just be "while play" since it's a boolean but let's keep it simple) and added an 'else if' just before that, which changed play to true.
- Added the bolded lime-coloured parts to implement winning.
Aditionally the following examples do the same:
Java Code:[B]Example 1:[/B] if (number > 100 || number == 100) { //Do whatever } [B]Example 2:[/B] if (number >= 100) { //Do whatever }
Last edited by Muskar; 11-21-2010 at 10:00 PM. Reason: Added colours
- 11-21-2010, 09:57 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
:eek: wow! thank you very much!! definitely makes a lot more sense. my code did seem kinda confusing. thanks again.
- 11-21-2010, 09:58 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
-
- 11-21-2010, 10:08 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
@Muskar : it does. my syntax was confusing me. thanks again.
@Fubarable : sorry! ill make note of that in the future.
- 11-21-2010, 10:14 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
Similar Threads
-
Looping
By Dean29126 in forum New To JavaReplies: 3Last Post: 09-08-2010, 02:01 PM -
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 10:54 PM -
Program Issue
By coder09 in forum New To JavaReplies: 5Last Post: 01-27-2009, 01:36 AM -
[SOLVED] Issue with Using Runtime.exec to run an external program
By coolFrenzi in forum Advanced JavaReplies: 1Last Post: 11-14-2008, 06:56 AM -
Class Scanner looping issue
By Stev0 in forum New To JavaReplies: 1Last Post: 05-25-2008, 06:53 PM
Bookmarks