Results 1 to 1 of 1
- 12-05-2012, 07:05 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
Noob Question on simple Rock Paper Scissors
Please lend me your powers guys!!
im actually having problem for this loop.. all i want is when i type a wrong or "Invalid choice" it will loop to the "How many rounds do i have" ..
we'll. just look at my codes here..
I hope you guys can help me.gif)
Java Code:import java.util.Scanner; public class RPS { public static void main (String [] args){ Scanner console = new Scanner (System.in); char p1, p2 = 0, repeat = 0; int round; int c,pts1=0,pts2=0; String player1; String player2; System.out.println(" Welcome to Rock, Paper, Scissor Game\n\n"); System.out.print("Enter Player 1 nickname: "); player1 = console.nextLine(); System.out.println("Player 1 name is "+ player1 + "..."); System.out.print ("Enter Player 2 name: "); player2 = console.nextLine(); System.out.println("Player 2 nickname is "+ player2 + "..."); do { c = 1; round = 0; pts1 = 0; pts2 = 0; System.out.print("How many rounds do you want?: "); round = console.nextInt(); System.out.println ("\nchoose R = Rock, S = Scissor, P = Paper"); while (c <= round) { System.out.print("\n Round "+ c++); System.out.println(" Fight!!!\n"); System.out.print (player1+ "'s turn: "); p1 = console.next().charAt(0); if (p1 == 'r' || p1 == 'R' || p1 == 'p' || p1 == 'P' || p1 == 's' || p1 == 'S' && p2 == 'r' || p2 == 'R' || p2 == 'p' || p2 == 'P' || p2 == 's' || p2 == 'S'){ } else {System.out.print(" Invalid Character! start from the top again!!\n"); System.exit(0);} System.out.print (player2+"'s turn: "); p2 = console.next().charAt(0); if (((p1 == 'r') || (p1 == 'R')) && ((p2 == 'r') || (p2 == 'R'))) { System.out.println(" DRAW! Nobody wins.\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 'p') || (p1 =='P')) && ((p2 == 'p') || (p2 == 'P'))) { System.out.print(" DRAW! Nobody wins.\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 's') || (p1 =='S')) && ((p2 == 's') || (p2 == 'S'))) { System.out.print(" DRAW! Nobody wins.\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 's') || (p1 =='S')) && ((p2 == 'p') || (p2 == 'P'))) { System.out.print(" Scissors cut the paper\n"); pts1++; System.out.print(" " + player1 + " wins!\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 'p') || (p1 =='P')) && ((p2 == 's') || (p2 == 'S'))) { System.out.print(" Scissors cut the paper\n"); pts2++; System.out.print(" " + player2 + " wins!\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 'p') || (p1 =='P')) && ((p2 == 'r') || (p2 == 'R'))) { System.out.print(" Paper Covered the Rock\n"); pts1++; System.out.print(" " + player1 + " wins!\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 'r') || (p1 =='R')) && ((p2 == 'p') || (p2 == 'P'))) { System.out.print(" Paper Covered the Rock\n"); pts2++; System.out.print(" " + player2 + " wins!\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 'r') || (p1 =='R')) && ((p2 == 's') || (p2 == 'S'))) { System.out.print(" Scissors had been broken by the Rock\n"); pts1++; System.out.print(" " + player1 + " wins!\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else if (((p1 == 's') || (p1 =='S')) && ((p2 == 'r') || (p2 == 'R'))) { System.out.print(" Scissors had been broken by the Rock\n"); pts2++; System.out.print(" " + player2 + " wins!\n"); System.out.print("Score: \n" + player1+"'s points = " + pts1 + "\n"+ player2 +"'s points = " + pts2); } else { System.out.print(" Invalid Character! Start from the top again!!\n"); System.exit(0); } } System.out.print("\n\n\tAND THE CHAMPION IS... "); if (pts1 > pts2){ System.out.println(player1 +"!! with the score of "+ pts1 +" - "+ pts2 + "\n CONGRATULATIONS!!"); System.out.print(" Better Luck next time "+ player2); } else if (pts1 < pts2){ System.out.println(player2 + "!! with the score of "+ pts2 +" - "+ pts1 + " \n CONGRATULATIONS!!"); System.out.print(" Better Luck next time "+ player1); } else { System.out.println(" DRAW!! No winner"); } System.out.print("\nDo you wish to continue the game? \n Y - Yes : N - No: "); repeat = console.next().charAt(0); }while ((repeat == 'y') || (repeat == 'Y')); } }
Similar Threads
-
Rock Paper Scissors Code...
By Shaeman111 in forum New To JavaReplies: 8Last Post: 02-08-2012, 03:20 AM -
Rock paper scissors.
By katie_gsu in forum New To JavaReplies: 7Last Post: 11-29-2011, 02:22 AM -
Rock, paper, scissors
By StevenF in forum New To JavaReplies: 3Last Post: 01-30-2011, 11:38 PM -
Rock Paper Scissors
By 54byler in forum Advanced JavaReplies: 2Last Post: 04-23-2009, 06:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks