Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!border3.nntp.ams.giganews.com!backlog3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 19 Jul 2014 02:19:16 -0500 From: leif.roar@invalid.com (Leif Roar Moldskred) Subject: Re: Help with paper, rock, scissors game Newsgroups: comp.lang.java.programmer References: User-Agent: tin/2.2.0-20131224 ("Lochindaal") (UNIX) (Linux/3.8.0-25-generic (i686)) Message-ID: Date: Sat, 19 Jul 2014 02:19:16 -0500 Lines: 135 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-ddzBvW7w0jhN80DfW+HUVAc2eqZ0tKS1Qza8+jLQVDkVALeAvug1MB+o4lX6A9UGfmVOxWkYPF3Oaex!OpfX73FbY6oo/+cr3BW2mxSUzzwPNpRV+rENWew7SRNpWpzlv4tg1WZawOjT1hKnmEgvrksOqcT6!1A== X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5888 Xref: csiph.com comp.lang.java.programmer:29121 scottwd01@gmail.com wrote: > > > 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. The overall gist of your program is right, but you have a number of syntax errors so that you get 17 errors when you add the missing close bracket is not unexpected. One tip, particularly when you start out, is to label your close bracket -- like this: public class Example { public static void main( String[] args ) { if( 1 > 2 ) { } // end if -- mathematical impossibility else { } // end else -- mathematical certainity: always happens } // end main() } // end class Example I've not got a java compiler at hand, so I might overlook a few errors, but here's what I've eyeballed of errors: > char r = R; > char p = P; > char s = S; This bit is wrong. Just R, P and S without any quotation marks around them would be a reference to _variables_ namend R, P and S, and as these don't exist the program will complainq. When you mean the _character_ R, you need to enclose it in single quotes like this: char r = 'R'; Of course, as you don't use the variables r, p or s anywhere in the program, you don't actually need these three lines at all. > > //Start of while loop that asks p1 and p2 for R,P or S. > while (playCount < 3); This bit is wrong -- the semicolon marks an end of the while loop, and the following open bracket then starts a block of code that has nothing to do with the while. basically, you've written this: while( playCount < 3 ) { /* Do nothing */ } { System.out.print("Player 1: Please enter either (R)ock, (P)aper, or (S)cissors: "); player1 = scan.nextLine() .toUpperCase() .charAt(0); ... } As playCount is never incremented inside the while loop, the loop will never exit but run forever. > //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 ; > } Here you have used the wrong quotation marks. In some languages, both single and double quotation marks can be used to enclose strings -- in Java there's a difference. Double quotation marks enclose strings, and single quotation marks enclose chars. Since player1 and player2 are variables of type char, you need to use single quotation marks here: if( player1 == 'P' ) { ... } > 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 ; > } // [1] > } // [2] Now, the close bracket I've annotated with [1] closes the last "else if" and the one I've annotated with [2] closes the method, so you need one more to close the class. Additionaly, as written, the scoring method has a problem in that it doesn't return a value for every possibile case, so that won't compile. If player1 enters something besides 'P', 'R' or 'S' you'll "skip over" the if, the first else if and the second else if, and hit the end of the method where there's no return statement. You'll want to either throw an exception or return an error value at that point. (If you choose to return an error value you should check for that value in the main method.) Hope this helps. P.S. You might get some posters here who will complain about the style of your code (how it's formatted and how it looks rather than what it does.) Don't worry about that -- right now you should focus on getting the code to do what you want it to do. Programming style is important later, but right now it'll just be one more thing to confuse you.