Hei! Jeg er blodig fersk i Java(åpnet BlueJ for 2 dager siden) og driver på med en innlevering hvor formålet er å definere klassen Player(med diverse metoder osv). Føler jeg kom meg greit gjennom brorparten av oppgaven helt til jeg begynte på siste delen.
Ved første øyekast så dette ut som noe av det "mindre krevende" ved oppgaven, men nå sitter jeg virkelig helt fast til tross for å ha studert boken og utallige nettsider. Forstår forsåvidt formålet til hovedmetoden, klarer bare ikke å forstå hvordan jeg skal formulere det slik at jeg får noe output. All form for veiledning/hjelp settes ufattelig stor pris på!
"Det skal opprettes en hovedmetode som oppretter en spiller og så skriver ut informasjonen"
Vis hele sitatet...
Kode
public class Player { // The nick of the player private String nick; // The type of the player private String type; // The health of the player private int health; /** * Create a new player with a nick and type(warrior,mage,rogue or any other you can think of) */ public Player(String setNick, String setType) { // prompts user for parameters to initialize object nick = setNick; type = setType; health = 100; } /** * Choose a nick */ public void setNick(String setNick) { // sets the nick of the player nick=setNick; } /** * Return the nick of the player */ public String getNick() { // returns the nick of the player return nick; } /** * Choose a type */ public void setType(String setType) { // sets the type of the player type=setType; } /** * Return the type of the player */ public String getType() { // returns the type of the player return type; } /** * Check the health of the player */ public int getHealth() { // returns the health of the player return health; } /** * Change the health of the player */ public void changeHealth(int amount) { // change the health of the player(health cannot exceed 100 or be less than 0) if(health + amount >= 0 && health + amount <= 100) { health = health + amount; } else { System.out.println("ERROR! Health cannot exceed 100 or be less than 0"); } } /** * Check if player is alive */ public void isAlive() { // checks if player health is greater than 0 and less or equal to 100 if(health > 0 && health <= 100) { System.out.println("Player is alive!! "); } else { System.out.println("Player is either dead!! "); } } /** * Print the players information */ public void print() { // prints the current nick,type and health of the player System.out.println("====================="); System.out.println(("Nick € ") + nick); System.out.println("_____________________"); System.out.println(("Type € ") + type); System.out.println("_____________________"); System.out.println(("Health € ") + health); System.out.println("====================="); // prints the current status of the player, either alive or kia(killed in action) if(health > 0 && health <= 100) { System.out.println("STATUS : ALIVE"); } else { System.out.println("STATUS : K.I.A"); } } /** * Main method */ public static void main(String[] args) { Player player = new Player(args[0], args[1]); System.out.println(player); } }