Hello,
I have a couple quick questions. First I just want to make sure I’m in the right forum? I am taking a class in college and it’s beginning Java.
I had a paper rock, scissors game as an assignment that was due a couple days ago. I ended up having to submit and not working file but, I’d like to figure out what I’m doing wrong for future assignments.
I feel like it’s correct and I got it down to just one error (reached end of file while parsing). I think that means that it needs an extra } at the end, but if I add one I go from one to 17 errors. I’m thinking maybe there is something wrong with my while loop or how I created a new function (or method in java, I had a class in python last semester so I’m not 100% sure I got the functions in java down yet). What makes sense to me and how I understand it is, the function itself must be created outside of main, but actually called in main? Also, are all of the functions going to be in the class RPS_Game? We haven’t really learned classes yet this is just my second week.
Here is my code, any help is greatly appreciated!
import java.util.Random; import java.util.Scanner; public class RPS_Game { public static void main(String[] args) { //Variables char player1, player2; char r = R; char p = P; char s = S; int winner, p1Score, p2Score, playCount; //Setting the p1 and p2 and playCount equal to 0. p1Score = p2Score = playCount = 0; Scanner scan = new Scanner(System.in); //Start of while loop that asks p1 and p2 for R,P or S. while (playCount < 3); { System.out.print("Player 1: Please enter either (R)ock, (P)aper, or (S)cissors: "); player1 = scan.nextLine() .toUpperCase() .charAt(0); System.out.print("Player 2: Please enter either (R)ock, (P)aper, or (S)cissors: "); player2 = scan.nextLine().toUpperCase() .charAt(0); //Add one to the playCount playCount ++; //Calling the winningPlayer function int winner = winningPlayer(player1, player2); if (winner = 0) { System.out.println("Tie! Nobody wins."); } else if (winner = 1) { System.out.println("Player 1 wins!"); p1Score ++; } else if (winner = 2) { System.out.println("Player 2 wins"); p2Score ++; } System.out.println("\nScores after this play:\nPlayer 1:" + p1score + "\nPlayer 2:" + p2Score + "\nThanks for playing!"); } } //Defining the winningPlayer function that decides which player wins or if there is a tie. public static int winningPlayer(char player1, char player2) { if (player1 == "P") { if (player2 == "P") return 0; else if (player2 == "R") return 1; else return 2 ; } else if (player1 == "R") { if (player2 == "R") return 0; else if (player2 == "S") return 1; else return 2; } else if (player1 == "S") { if (player2 == "S") return 0; else if (player2 == "P") return 1; else return 2 ; } }