|
Until you sign up you can't do much. Yes, it's free.
|
|
|
|
|
|
|
 | / / / Viewing Topic
|  |
|
|
Print Statements and Scanners (Java) |
|
|
|
Replies: 10 Last Post Oct. 31, 2008 8:58pm by Majo
|
|
|
|
|
( Majo )
Soothsayer
|
| 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.
------- "Under the hardness of her facade a woman's heart is still beating."
|
7:17 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 301 Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 10,531
|
|
| |
|
|
|
|
 LiveWire Humor
|
|
noraa
Soothsayer
Sustainer
|
:heart attack:
|
7:18 pm on Oct. 29, 2008 | Joined: Sep. 2007 | Days Active: 442 Join to learn more about noraa New York, United States | Label Free Female | Posts: 8,000 | Points: 11,214
|
|
| |
|
|
|
|
|
|
|
|
( Majo )
Soothsayer
|
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; } } |
------- "Under the hardness of her facade a woman's heart is still beating."
|
7:27 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 301 Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 10,531
|
|
| |
|
|
|
|
|
|
|
|
matto
Omnipotent One
Patron
Tech Support Leader
|
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 ...
------- "Say what you like about the tenets of National Socialism, Dude, at least it's an ethos." --Walter Sobchak
|
12:54 am on Oct. 30, 2008 | Joined: Aug. 2007 | Days Active: 490 Join to learn more about matto California, United States | Male | Posts: 9,804 | Points: 19,066
|
|
| |
|
|
|
|
|
| Looking for something else?
|
|
|
|
|
|
 | / / / Viewing Topic |  |
|