Results 1 to 6 of 6
Thread: Working with Methods
- 03-25-2010, 04:17 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
Working with Methods
Here's my code:
I need help with the main method. If the fightInput is valid, then the user will be asked whether or not he/she wants to continue. If it's N, then the game will end: playerHealth and computerHealth will be displayed and whatnot. If it's Y, then it will start from the beginning, where it asks the user for the move. This process should be repeated forever until the user enters N. I need help with this part :(.Java Code:import java.io.*; import java.util.*; public class lab6 { public static void main(String [] args) { Scanner input = new Scanner(System.in); // Initial health amounts int playerHealth = 50; int computerHealth = 50; // Welcome junk System.out.println("Welcome! Please enter your name."); String playerName = input.nextLine(); System.out.println("Please choose a fight move (name)."); System.out.println("1. punch"); System.out.println("2. throw"); System.out.println("3. block"); String fightInput = input.nextLine(); boolean valid = checkInput(fightInput); while(valid == false) { System.out.println("Invalid input. Please try again."); System.out.println("Please choose a fight move (name)."); System.out.println("1. punch"); System.out.println("2. throw"); System.out.println("3. block"); fightInput = input.nextLine(); valid = checkInput(fightInput); } if(valid == true) { System.out.println("You entered "+fightInput+". Would you like to continue fighting (y/n)?"); String yORn = input.nextLine(); if(yORn.equals("n")) { System.out.println("Player health: "+playerHealth+""); System.out.println("Computer health: "+computerHealth+""); if(playerHealth > computerHealth) { System.out.println("Player ("+playerName+") wins!"); System.out.println("Thanks for playing."); } else { System.out.println("Computer wins!"); System.out.println("Thanks for playing."); } } if(yORn.equals("y")) { System.out.println("Please choose a fight move (name)."); System.out.println("1. punch"); System.out.println("2. throw"); System.out.println("3. block"); fightInput = input.nextLine(); valid = checkInput(fightInput); } } } public static boolean checkInput(String fightInput) { if(fightInput.equals("punch")) { return true; } else if(fightInput.equals("throw")) { return true; } if(fightInput.equals("block")) { return true; } else { return false; } } public static void calcComputerMove(int computerInput) { int computerMoveNumber = (int)(Math.random()*10); if(computerMoveNumber == 0 || computerMoveNumber == 1 || computerMoveNumber == 2 || computerMoveNumber == 3) { String computerMove = "punch"; } else if(computerMoveNumber == 4 || computerMoveNumber == 5 || computerMoveNumber == 6) { String computerMove = "throw"; } else if(computerMoveNumber == 7 || computerMoveNumber == 8 || computerMoveNumber == 9) { String computerMove = "block"; } } public static int calcPlayerHealth(int playerHealth, String fightInput, String computerMove) { if(fightInput.equals("punch") && computerMove.equals("block")) playerHealth -= 2; else if(fightInput.equals("block") && computerMove.equals("throw")) playerHealth -= 4; else if(fightInput.equals("throw") && computerMove.equals("punch")) playerHealth -= 6; else playerHealth -= 0; return playerHealth; } public static int calcComputerHealth(int computerHealth, String fightInput, String computerMove) { if(computerMove.equals("punch") && fightInput.equals("block")) computerHealth -= 2; else if(computerMove.equals("block") && fightInput.equals("throw")) computerHealth -= 4; else if(computerMove.equals("throw") && fightInput.equals("punch")) computerHealth -= 6; else computerHealth -= 0; return computerHealth; } }
I also need to get the health reduction/display working correctly. I'm not sure how to make a outside method work with main properly.
- 03-25-2010, 05:05 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So what happen, I can see that you've implement something related to that condition? Did you test it, and if so what happen? Any error message? Can you please answer to those questions.
- 03-25-2010, 05:08 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
The way it is now, there are no errors. After entering in the first move, it will ask Y/N.
If N, the it will display the health for both players as well as the winner. Although the health that is displayed is the same as the initial values (they aren't being decreased like they should be).
If Y, it will ask for another move. After that move is entered, the program will end itself.
- 03-25-2010, 05:16 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
This is all what you've to do. Why?
Java Code:while (valid == true) { System.out.println("You entered " + fightInput + ". Would you like to continue fighting (y/n)?"); String yORn = input.nextLine(); if (yORn.equals("n")) { System.out.println("Player health: " + playerHealth + ""); System.out.println("Computer health: " + computerHealth + ""); if (playerHealth > computerHealth) { System.out.println("Player (" + playerName + ") wins!"); System.out.println("Thanks for playing."); } else { System.out.println("Computer wins!"); System.out.println("Thanks for playing."); } valid = false; } else if (yORn.equals("y")) { System.out.println("Please choose a fight move (name)."); System.out.println("1. punch"); System.out.println("2. throw"); System.out.println("3. block"); fightInput = input.nextLine(); valid = checkInput(fightInput); } }
- 03-25-2010, 05:17 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please go through the changes and see what I've done.
Regarding your code, actually it's really bad practice in coding. See how many things you've repeated. Just take a paper and a pencil and write down the logic or the pseudo code of your class. Then you can find what you've really messup.
- 03-25-2010, 05:33 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
working with JC
By yuhobebbho in forum New To JavaReplies: 0Last Post: 02-10-2010, 11:22 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Static methods - not working
By Echilon in forum New To JavaReplies: 2Last Post: 12-21-2007, 01:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks