|
-- Posted by Majo at 7:17 pm on Oct. 29, 2008
| Code: | | import java.util.*; public class PlayerTest { public static void main(String[] args) { Player player1 = new Player(); Player player2 = new Player(); Scanner scn = new Scanner(System.in); System.out.print("Please enter name of the first player: "); String name = scn.nextLine(); player1.setPlayerName(name); System.out.println(); int score1; System.out.print("Please enter scores for player one.\n"); for ( ;;) { System.out.print("Please enter score #1: "); score1 = scn.nextInt(); if(player1.validateScore(score1)) { break; } System.out.println("Invalid score, range 0-100."); } int score2; for( ;; ) { System.out.print("Please enter score #2: "); score2 = scn.nextInt(); if(player1.validateScore(score2)) { break; } System.out.println("Invalid score, range 0-100."); } int score3; for( ;; ) { System.out.print("Please enter score #3: "); score3 = scn.nextInt(); if(player1.validateScore(score3)) { break; } System.out.println("Invalid score, range 0-100."); } System.out.print("Please enter name of the second player: "); String p2Name = scn.nextLine(); player2.setPlayerName(p2Name); System.out.println(); int p2Score1; System.out.print("Please enter scores for player two."); for( ;; ) { System.out.print("Please enter score #1: "); p2Score1 = scn.nextInt(); if(player2.validateScore(p2Score1)) { break; } System.out.println("Invalid score, range 0-100."); } int p2Score2; for( ;; ) { System.out.print("Please enter score #2: "); p2Score2 = scn.nextInt(); if(player2.validateScore(p2Score2)) { break; } System.out.println("Invalid score, range 0-100."); } int p2Score3; for( ;; ) { System.out.print("Please enter score #3: "); p2Score3 = scn.nextInt(); if(player2.validateScore(p2Score3)) { break; } System.out.println("Invalid score, range 0-100."); } System.out.print("\nThe scores for " + player1.getPlayerName() + " are " + score1 + ", " + score2 + ", " + score3 + "; for a total of "); System.out.print("\nThe scores for " + player2.getPlayerName() + " are " + p2Score1 + ", " + p2Score2 + ", " + p2Score3 + "; for a total of "); System.out.print("\nThe highest average score was " + " by "); System.out.print("\nThe highest game score was"); } } | I'm supposed to prompt the user to enter the second player's name using a print statement and scanner. I thought I had it set up correctly but it prints out the prompt and then moves on, never allowing me to actually enter a name.
-- Posted by noraa at 7:18 pm on Oct. 29, 2008
:heart attack:
-- Posted by Majo at 7:20 pm on Oct. 29, 2008
Quote: from NoNoNora383 at 10:18 pm on Oct. 29, 2008
:heart attack:
Yeah, I know. And that's a small program. They get much bigger. Oh, and it's not done quite yet.
-- Posted by taraxgoesxboom at 7:21 pm on Oct. 29, 2008
It's kinda hard for me to look at this without seeing an assignment sheet. Why are you using player2.setPlayerName(p2Name); this statement?
-- Posted by taraxgoesxboom at 7:26 pm on Oct. 29, 2008
System.out.print("Please enter name of the second player: "); It might be a syntax error, you forgot the ln on this line. Are you using J creator? It wont compile it unless there are no syntax errors.
-- Posted by Majo at 7:27 pm on Oct. 29, 2008
Quote: from taraxgoesxboom at 10:21 pm on Oct. 29, 2008
It's kinda hard for me to look at this without seeing an assignment sheet. Why are you using player2.setPlayerName(p2Name); this statement? 
Sorry. | Code: | public class Player { private String name; private int score1; private int score2; private int score3; public Player() { name = "No Name"; score1 = 0; score2 = 0; score3 = 0; } public int getScore1() { return score1; } public int getScore2() { return score2; } public int getScore3() { return score3; } public String getPlayerName() { return name; } public void setPlayerName(String _name) { name = _name; } public void setScore1(int _score1) { score1 = _score1; } public void setScore2(int _score2) { score2 = _score2; } public void setScore3(int _score3) { score3 = _score3; } public boolean validateScore(int score) { if((score >= 0) && (score <= 100)) { return true; } return false; } public int calcTotal() { return score1+score2+score3; } public double calcAverage() { int total = calcTotal(); return (double)total/3.0; } public int calcHighScore() { int high = score1; if(score2 > high) { high = score2; } if(score3 > high) { high = score3; } return high; } } |
-- Posted by Majo at 7:28 pm on Oct. 29, 2008
Quote: from taraxgoesxboom at 10:26 pm on Oct. 29, 2008
System.out.print("Please enter name of the second player: "); It might be a syntax error, you forgot the ln on this line. Are you using J creator? It wont compile it unless there are no syntax errors. 
No, I'm using jGRASP. I think I've tried println but I'll try it again.
-- Posted by taraxgoesxboom at 7:33 pm on Oct. 29, 2008
Quote: from Majo at 7:28 pm on Oct. 29, 2008
Quote: from taraxgoesxboom at 10:26 pm on Oct. 29, 2008
System.out.print("Please enter name of the second player: "); It might be a syntax error, you forgot the ln on this line. Are you using J creator? It wont compile it unless there are no syntax errors. 
No, I'm using jGRASP. I think I've tried println but I'll try it again. 
Sorry, other than that I don't really see what it's missing. Right now I'm working on arrays and their still kicking my butt, lol. Good luck.
-- Posted by Majo at 7:36 pm on Oct. 29, 2008
println didn't work. I don't know what's wrong. We're working on arrays, too. They're kicking my ass as well. 
-- Posted by matto at 12:54 am on Oct. 30, 2008
Can you post it with indents? Hella hard to troubleshoot when it's like that. Also, if I were writing such a program, I'd implement a method called getInfo() or something within Player.java that input a Player object and prompted the user for and properly stored all of the information. It'd shorten the amount of code significantly, and make troubleshooting much easier (if required at all). Then your main would look something like Player player1 = new Player(); Player player2 = new Player(); player1.getInfo(); player2.getInfo(); System.out.println(player1); System.out.println(player2); If you put a toString() method in Player.java, then you can do the above. System.out.println(player1) will print out the value of player1.toString() if such a method exists, also reducing the redundancy of your code. My bad if this is hella useless information. I dunno what's wrong with your code because reading it is a bitch and it's difficult to find problems with it in its current, unindented form. So yeah. You in AP CS? It's teh shit I'm in that class right now ...
-- Posted by Majo at 8:58 pm on Oct. 31, 2008
I'm in Programming for Info Tech I. Sorry about the indents thing, I tried but every time I hit tab, my cursor jumped from within the text box to some other point on the screen so I gave up. As it turns out, taking out Line in scn.nextLine() did it. I have no idea why though.
|