I'm attempting to code an aid for when my little brother and I play a game, and I'm getting a weird, non-fatal, error that you'll see at the end. My main client code attempts to generate two objects, PlayerOne and PlayerTwo. Generating PlayerOne goes just fine, but when I generate PlayerTwo, it just skips lines of code, crucial to the object. My main code is as follows:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package classes; import java.util.*; /** * * @author DaedricSheep */ public class Duel { public static void main(String[] args) { String nameOne, nameTwo; int LP1, LP2; Scanner input = new Scanner(System.in); System.out.println("Name of Player 1: "); nameOne = input.nextLine(); System.out.println("How many Life Points to start?"); LP1 = input.nextInt(); Player PlayerOne = new Player(nameOne, LP1); System.out.println(PlayerOne.toString()); System.out.println(" "); System.out.println("Name of Player 2: "); nameTwo = input.nextLine(); System.out.println("How many Life Points to start?"); LP2 = input.nextInt(); Player PlayerTwo = new Player(nameTwo,LP2); System.out.println(PlayerTwo.toString()); System.out.println(""); boolean victory = false; while (victory == false){ System.out.println("IT'S TIME TO DUEL! (type 'help' for options)"); String action = input.nextLine(); switch (action){ case "help": System.out.println("Actions:"); System.out.println("help, damage, restore, flip coin, guess coin, roll die, victory"); System.out.println(""); break; case "damage": System.out.println("Was damage was done to player 1 or 2? (enter '1' or '2'"); int playerDamage = input.nextInt(); if (playerDamage==1){ System.out.print("Damage dealt: "); int dealtDam = input.nextInt(); PlayerOne.damage(dealtDam); PlayerOne.toString(); System.out.println(""); }else if(playerDamage==2){ System.out.print("Damage dealt: "); int dealtDam = input.nextInt(); PlayerTwo.damage(dealtDam); PlayerTwo.toString(); System.out.println(""); } break; case "restore": System.out.println("Is player 1 or player 2 being healed? (enter '1' or '2'"); int playerHeal = input.nextInt(); if(playerHeal==1){ System.out.print("Health restored: "); int healPoints = input.nextInt(); PlayerOne.restore(healPoints); PlayerOne.toString(); System.out.println(""); }else if(playerHeal==2){ System.out.print("Health restored: "); int healPoints = input.nextInt(); PlayerTwo.restore(healPoints); PlayerTwo.toString(); System.out.println(""); } break; case "flip coin": break; case "guess coin": break; case "roll die": InGameActions.dieRoll(); break; } } } }
and it refers to the generation of what follows here:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package classes; /** * * @author DaedricSheep */ public class Player { String playername; int LP; public Player(String name, int Life){ playername = name; LP = Life; } public int damage(int damage){ LP = LP - damage; return(LP); } public int restore(int restore){ LP = LP + restore; return(LP); } public String returnName(){ return(playername); } public int returnPoints(){ return(LP); } @Override public String toString(){ String that = ("Name: "+returnName()+" Life Points: "+returnPoints()); return(that); } }
When I run the program, this happens: