Skip to main content
stats tables
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Stats tables

A simple way to condense your stats to a table-like format could look like

 public static void printStats(Character... chars) { System.out.print(" ".repeat(9)); for (Character col: chars) System.out.printf("%8s ", col); System.out.println(); System.out.print("-".repeat(8)); for (Character col: chars) System.out.print("-".repeat(9)); System.out.println(); System.out.printf("%8s ", "Health"); for (Character col: chars) System.out.printf("%8d ", col.health); System.out.println(); System.out.printf("%8s ", "Defense"); for (Character col: chars) System.out.printf("%8d ", col.defense); System.out.println(); System.out.printf("%8s ", "Strength"); for (Character col: chars) System.out.printf("%8d ", col.strength); System.out.printf("%n%n"); } 

called like

Character.printStats(hero, enemy); 

with output

 Kirk Spock -------------------------- Health 78 42 Defense 99 5 Strength 93 97 

Stats tables

A simple way to condense your stats to a table-like format could look like

 public static void printStats(Character... chars) { System.out.print(" ".repeat(9)); for (Character col: chars) System.out.printf("%8s ", col); System.out.println(); System.out.print("-".repeat(8)); for (Character col: chars) System.out.print("-".repeat(9)); System.out.println(); System.out.printf("%8s ", "Health"); for (Character col: chars) System.out.printf("%8d ", col.health); System.out.println(); System.out.printf("%8s ", "Defense"); for (Character col: chars) System.out.printf("%8d ", col.defense); System.out.println(); System.out.printf("%8s ", "Strength"); for (Character col: chars) System.out.printf("%8d ", col.strength); System.out.printf("%n%n"); } 

called like

Character.printStats(hero, enemy); 

with output

 Kirk Spock -------------------------- Health 78 42 Defense 99 5 Strength 93 97 
verb tenses
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Avoid static abuse on your outer functions. Most of your Game methods should be instance methods with access to the member variables they need.

Various spelling issues like croud (should be crowd); enable spellcheck. Make your verb tenses agree - they should all be in present tense, instead of a mix of past and present tense.

package com.stackexchange; import java.util.Optional; import java.util.Random; import java.util.Scanner; public class Game { public record Hit( boolean blocked, int attack_points ) { @Override public String toString() { if (attack_points <=== 0) return "no damage";  if (attack_points == 1) return "1 point of damage";  return "%d points of damage".formatted(attack_points); } } public static class Character { private int health; private int defense; publicprivate final int strength; public final String name; public Character(String name, Random rand) { health = rand.nextInt(1, 100); defense = rand.nextInt(1, 100); strength = rand.nextInt(1, 100); this.name = name; } public boolean isAlive() { return health > 0; } public Hit receiveHit(Character attacker) { int attack_points = Integer.max(0, attacker.strength - defense); boolean blocked = attack_points == 0; if (blocked) defense -= (defense % attacker.strength) + 1; health = Integer.max(0, health - attack_points); return new Hit(blocked, attack_points); } private void printStats() { System.out.printf("%s's Stats:%n", name); System.out.println("---------------"); final String fmt = "%-10s %-3d%n";2d%n"; System.out.printf(fmt, "Health:", health); System.out.printf(fmt, "Defense:", defense); System.out.printf(fmt, "Strength:", strength); System.out.println(); } @Override public String toString() { return name; } } private final Random rand = new Random(); private final Scanner scanner = new Scanner(System.in); private final Character hero; private final Character enemy = new Character("Spock", rand); public Game() { System.out.println("Welcome to the arena!"); System.out.print("Enter your hero's name: "); hero = new Character(scanner.nextLine(), rand);  System.out.printf("Avast, %s! Go forth!%n", hero);  } public void clash() { System.out.printf("%s tooktakes a cheapshotcheap shot!%n", enemy); System.out.println("(Crowd gasps)"); System.out.printf("But %s blockedblocks it in the nick of time!%n", hero); System.out.println("(Crowd cheers)"); } public Optional<Character> rollInitiative() { int hero_initiative = rand.nextInt(1, 7), enemy_initiative = rand.nextInt(1, 7); if (hero_initiative > enemy_initiative) return Optional.of(hero); if (hero_initiative < enemy_initiative) return Optional.of(enemy); return Optional.empty(); // tie ("cheap shot") } public void attack(Character attacker, Character defender) { Hit hit = defender.receiveHit(attacker); if (hit.blocked) { System.out.printf("%s blocks %s's attack and takes %s!%n", defender, attacker, hit); System.out.println("(Crowd cheers)"); } else { System.out.printf("%s strikes %s for %s!%n", attacker, defender, hit); System.out.println("(Crowd boos)"); } } public void doBattle() { Optional<Character> goesFirst = rollInitiative(); if (goesFirst.isPresent()) { Character attacker = goesFirst.get(); System.out.printf("%s takes initiative!%n", attacker); Character defender = hero == attacker? enemy: hero; attack(attacker, defender); } else { clash(); // tie ("cheap shot") } } public void run() {  System.out.printf("Avast, %s! Go forth!%n", hero);  do { System.out.println(); hero.printStats(); enemy.printStats(); if (!hero.isAlive()) { System.out.printf("%s defeateddefeats %s!%n", enemy, hero); System.out.println("(Crowd boos aggressively)"); System.out.println("Someone from the crowd yelledyells \"YOU SUCK!\""); break; } if (!enemy.isAlive()) { System.out.printf("%s utterly smotesmites %s!%n", hero, enemy); System.out.println("(Crowd ROARS)"); break; } } while (doRound()); } private boolean doRound() { while (true) { System.out.print("Enter option (1 to battle, 2 to escape)! "); switch (scanner.nextLine()) { case "1": doBattle(); return true; case "2": System.out.println("YOU COWARD!"); return false; default: System.err.println("Invalid option"); } } } public static void main(String[] args) { new Game().run(); } } 
Welcome to the arena! Enter your hero's name: JulioKirk Avast, JulioKirk! Go forth! Julio'sKirk's Stats: --------------- Health:  69 68 Defense: 59  Strength: 62 50 Spock's Stats: --------------- Health:  99 56 Defense: 323 Strength:  85 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes initiativea cheap shot! Spock(Crowd strikesgasps) But JulioKirk forblocks 26it pointsin the nick of damagetime! (Crowd booscheers) Julio'sKirk's Stats: --------------- Health:  43 68 Defense: 59  Strength: 62 50 Spock's Stats: --------------- Health: 99 56 Defense: 323 Strength: 85 49 Enter option (1 to battle, 2 to escape)! 1 JulioKirk takes initiative! JulioKirk strikes Spock for 3047 points of damage! (Crowd boos) Julio'sKirk's Stats: --------------- Health:  43 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 6249 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers)  Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 699 Defense: 323 Strength: 8549 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers) Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 JulioSpock takes initiative! JulioKirk strikesblocks SpockSpock's forattack 30and pointstakes ofno damage! (Crowd booscheers) Julio'sKirk's Stats: --------------- Health: 4368 Defense: 48 Strength: 50 Spock's Stats: --------------- Health:  9  Defense: 593 Strength: 6249 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers) Kirk's Stats: --------------- Health: 68 Defense: 48 Strength: 50 Spock's Stats: --------------- Health: 399 Defense: 323 Strength:  85 49 Enter option (1 to battle, 2 to escape)! 1 JulioSpock takes initiative! JulioSpock strikes SpockKirk for 301 pointspoint of damage! (Crowd boos) Julio'sKirk's Stats: --------------- Health:  43 67 Defense: 59 48 Strength:  62 50 Spock's Stats: --------------- Health: 9  Defense: 323 Strength:  85 49 Enter option (1 to battle, 2 to escape)! 1 JulioKirk takes initiative! JulioKirk strikes Spock for 3047 points of damage! (Crowd boos) Julio'sKirk's Stats: --------------- Health: 43 67 Defense:  59 48 Strength:  62 50 Spock's Stats: --------------- Health: 0  Defense: 323 Strength: 85 49 JulioKirk utterly smotesmites Spock! (Crowd ROARS) 

Various spelling issues like croud (should be crowd); enable spellcheck.

package com.stackexchange; import java.util.Optional; import java.util.Random; import java.util.Scanner; public class Game { public record Hit( boolean blocked, int attack_points ) { @Override public String toString() { if (attack_points <= 0) return "no damage"; return "%d points of damage".formatted(attack_points); } } public static class Character { private int health; private int defense; public final int strength; public final String name; public Character(String name, Random rand) { health = rand.nextInt(1, 100); defense = rand.nextInt(1, 100); strength = rand.nextInt(1, 100); this.name = name; } public boolean isAlive() { return health > 0; } public Hit receiveHit(Character attacker) { int attack_points = Integer.max(0, attacker.strength - defense); boolean blocked = attack_points == 0; if (blocked) defense -= (defense % attacker.strength) + 1; health = Integer.max(0, health - attack_points); return new Hit(blocked, attack_points); } private void printStats() { System.out.printf("%s's Stats:%n", name); System.out.println("---------------"); final String fmt = "%-10s %-3d%n"; System.out.printf(fmt, "Health:", health); System.out.printf(fmt, "Defense:", defense); System.out.printf(fmt, "Strength:", strength); System.out.println(); } @Override public String toString() { return name; } } private final Random rand = new Random(); private final Scanner scanner = new Scanner(System.in); private final Character hero; private final Character enemy = new Character("Spock", rand); public Game() { System.out.println("Welcome to the arena!"); System.out.print("Enter your hero's name: "); hero = new Character(scanner.nextLine(), rand); } public void clash() { System.out.printf("%s took a cheapshot!%n", enemy); System.out.println("(Crowd gasps)"); System.out.printf("But %s blocked it in the nick of time!%n", hero); System.out.println("(Crowd cheers)"); } public Optional<Character> rollInitiative() { int hero_initiative = rand.nextInt(1, 7), enemy_initiative = rand.nextInt(1, 7); if (hero_initiative > enemy_initiative) return Optional.of(hero); if (hero_initiative < enemy_initiative) return Optional.of(enemy); return Optional.empty(); } public void attack(Character attacker, Character defender) { Hit hit = defender.receiveHit(attacker); if (hit.blocked) { System.out.printf("%s blocks %s's attack and takes %s!%n", defender, attacker, hit); System.out.println("(Crowd cheers)"); } else { System.out.printf("%s strikes %s for %s!%n", attacker, defender, hit); System.out.println("(Crowd boos)"); } } public void doBattle() { Optional<Character> goesFirst = rollInitiative(); if (goesFirst.isPresent()) { Character attacker = goesFirst.get(); System.out.printf("%s takes initiative!%n", attacker); Character defender = hero == attacker? enemy: hero; attack(attacker, defender); } else { clash(); } } public void run() {  System.out.printf("Avast, %s! Go forth!%n", hero);  do { System.out.println(); hero.printStats(); enemy.printStats(); if (!hero.isAlive()) { System.out.printf("%s defeated %s!%n", enemy, hero); System.out.println("(Crowd boos aggressively)"); System.out.println("Someone from the crowd yelled \"YOU SUCK!\""); break; } if (!enemy.isAlive()) { System.out.printf("%s utterly smote %s!%n", hero, enemy); System.out.println("(Crowd ROARS)"); break; } } while (doRound()); } private boolean doRound() { while (true) { System.out.print("Enter option (1 to battle, 2 to escape)! "); switch (scanner.nextLine()) { case "1": doBattle(); return true; case "2": System.out.println("YOU COWARD!"); return false; default: System.err.println("Invalid option"); } } } public static void main(String[] args) { new Game().run(); } } 
Welcome to the arena! Enter your hero's name: Julio Avast, Julio! Go forth! Julio's Stats: --------------- Health:  69 Defense: 59  Strength: 62  Spock's Stats: --------------- Health:  99 Defense: 32 Strength:  85 Enter option (1 to battle, 2 to escape)! 1 Spock takes initiative! Spock strikes Julio for 26 points of damage! (Crowd boos) Julio's Stats: --------------- Health:  43 Defense: 59  Strength: 62  Spock's Stats: --------------- Health: 99  Defense: 32 Strength: 85  Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health:  43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 69 Defense: 32 Strength: 85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 39 Defense: 32 Strength:  85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health:  43 Defense: 59  Strength:  62 Spock's Stats: --------------- Health: 9  Defense: 32 Strength:  85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43  Defense:  59 Strength:  62 Spock's Stats: --------------- Health: 0  Defense: 32 Strength: 85  Julio utterly smote Spock! (Crowd ROARS) 

Avoid static abuse on your outer functions. Most of your Game methods should be instance methods with access to the member variables they need.

Various spelling issues like croud (should be crowd); enable spellcheck. Make your verb tenses agree - they should all be in present tense, instead of a mix of past and present tense.

package com.stackexchange; import java.util.Optional; import java.util.Random; import java.util.Scanner; public class Game { public record Hit( boolean blocked, int attack_points ) { @Override public String toString() { if (attack_points == 0) return "no damage";  if (attack_points == 1) return "1 point of damage";  return "%d points of damage".formatted(attack_points); } } public static class Character { private int health; private int defense; private final int strength; public final String name; public Character(String name, Random rand) { health = rand.nextInt(1, 100); defense = rand.nextInt(1, 100); strength = rand.nextInt(1, 100); this.name = name; } public boolean isAlive() { return health > 0; } public Hit receiveHit(Character attacker) { int attack_points = Integer.max(0, attacker.strength - defense); boolean blocked = attack_points == 0; if (blocked) defense -= (defense % attacker.strength) + 1; health = Integer.max(0, health - attack_points); return new Hit(blocked, attack_points); } private void printStats() { System.out.printf("%s's Stats:%n", name); System.out.println("---------------"); final String fmt = "%-10s %-2d%n"; System.out.printf(fmt, "Health:", health); System.out.printf(fmt, "Defense:", defense); System.out.printf(fmt, "Strength:", strength); System.out.println(); } @Override public String toString() { return name; } } private final Random rand = new Random(); private final Scanner scanner = new Scanner(System.in); private final Character hero; private final Character enemy = new Character("Spock", rand); public Game() { System.out.println("Welcome to the arena!"); System.out.print("Enter your hero's name: "); hero = new Character(scanner.nextLine(), rand);  System.out.printf("Avast, %s! Go forth!%n", hero);  } public void clash() { System.out.printf("%s takes a cheap shot!%n", enemy); System.out.println("(Crowd gasps)"); System.out.printf("But %s blocks it in the nick of time!%n", hero); System.out.println("(Crowd cheers)"); } public Optional<Character> rollInitiative() { int hero_initiative = rand.nextInt(1, 7), enemy_initiative = rand.nextInt(1, 7); if (hero_initiative > enemy_initiative) return Optional.of(hero); if (hero_initiative < enemy_initiative) return Optional.of(enemy); return Optional.empty(); // tie ("cheap shot") } public void attack(Character attacker, Character defender) { Hit hit = defender.receiveHit(attacker); if (hit.blocked) { System.out.printf("%s blocks %s's attack and takes %s!%n", defender, attacker, hit); System.out.println("(Crowd cheers)"); } else { System.out.printf("%s strikes %s for %s!%n", attacker, defender, hit); System.out.println("(Crowd boos)"); } } public void doBattle() { Optional<Character> goesFirst = rollInitiative(); if (goesFirst.isPresent()) { Character attacker = goesFirst.get(); System.out.printf("%s takes initiative!%n", attacker); Character defender = hero == attacker? enemy: hero; attack(attacker, defender); } else { clash(); // tie ("cheap shot") } } public void run() { do { System.out.println(); hero.printStats(); enemy.printStats(); if (!hero.isAlive()) { System.out.printf("%s defeats %s!%n", enemy, hero); System.out.println("(Crowd boos aggressively)"); System.out.println("Someone from the crowd yells \"YOU SUCK!\""); break; } if (!enemy.isAlive()) { System.out.printf("%s utterly smites %s!%n", hero, enemy); System.out.println("(Crowd ROARS)"); break; } } while (doRound()); } private boolean doRound() { while (true) { System.out.print("Enter option (1 to battle, 2 to escape)! "); switch (scanner.nextLine()) { case "1": doBattle(); return true; case "2": System.out.println("YOU COWARD!"); return false; default: System.err.println("Invalid option"); } } } public static void main(String[] args) { new Game().run(); } } 
Welcome to the arena! Enter your hero's name: Kirk Avast, Kirk! Go forth! Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 56 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers) Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 56 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Kirk takes initiative! Kirk strikes Spock for 47 points of damage! (Crowd boos) Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers)  Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers) Kirk's Stats: --------------- Health: 68 Defense: 59 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes initiative! Kirk blocks Spock's attack and takes no damage! (Crowd cheers) Kirk's Stats: --------------- Health: 68 Defense: 48 Strength: 50 Spock's Stats: --------------- Health:  9  Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes a cheap shot! (Crowd gasps) But Kirk blocks it in the nick of time! (Crowd cheers) Kirk's Stats: --------------- Health: 68 Defense: 48 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Spock takes initiative! Spock strikes Kirk for 1 point of damage! (Crowd boos) Kirk's Stats: --------------- Health: 67 Defense: 48 Strength: 50 Spock's Stats: --------------- Health: 9 Defense: 3 Strength: 49 Enter option (1 to battle, 2 to escape)! 1 Kirk takes initiative! Kirk strikes Spock for 47 points of damage! (Crowd boos) Kirk's Stats: --------------- Health: 67 Defense: 48 Strength: 50 Spock's Stats: --------------- Health: 0 Defense: 3 Strength: 49 Kirk utterly smites Spock! (Crowd ROARS) 
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Make as many of the properties of Character to be final as possible. init (which should be named initiative) should not be a property at all.

Delete getRandom. Pass in an instance of Random for testability, and call its nextInt. Make Character a static inner class - it won't be able to access properties of Game, and that's a good thing - it's better to pass them in explicitly when needed.

cls (clearing the screen) is an anti-feature. The user can clear the screen when they want through their own terminal controls.

Don't \' escape apostrophes when that isn't needed.

There is no need for Console in this application.

Make more use of printf instead of string concatenation.

Don't abbreviate variables like h (hero) and e (enemy) to single letters; spell them out.

Various spelling issues like croud (should be crowd); enable spellcheck.

clash calling doBattle is recursion, which is not a good idea; you almost certainly shouldn't be recursing here.

Don't nextInt for your option. Since it's only used in a simple comparison, leave it as a string; it removes the potential for an exception and simplifies validation.

Whenever possible, construct your console formatting so that it produces logically-related paragraphs. Also, prefer accepting terminal input on the same line as the prompt.

Suggested

package com.stackexchange; import java.util.Optional; import java.util.Random; import java.util.Scanner; public class Game { public record Hit( boolean blocked, int attack_points ) { @Override public String toString() { if (attack_points <= 0) return "no damage"; return "%d points of damage".formatted(attack_points); } } public static class Character { private int health; private int defense; public final int strength; public final String name; public Character(String name, Random rand) { health = rand.nextInt(1, 100); defense = rand.nextInt(1, 100); strength = rand.nextInt(1, 100); this.name = name; } public boolean isAlive() { return health > 0; } public Hit receiveHit(Character attacker) { int attack_points = Integer.max(0, attacker.strength - defense); boolean blocked = attack_points == 0; if (blocked) defense -= (defense % attacker.strength) + 1; health = Integer.max(0, health - attack_points); return new Hit(blocked, attack_points); } private void printStats() { System.out.printf("%s's Stats:%n", name); System.out.println("---------------"); final String fmt = "%-10s %-3d%n"; System.out.printf(fmt, "Health:", health); System.out.printf(fmt, "Defense:", defense); System.out.printf(fmt, "Strength:", strength); System.out.println(); } @Override public String toString() { return name; } } private final Random rand = new Random(); private final Scanner scanner = new Scanner(System.in); private final Character hero; private final Character enemy = new Character("Spock", rand); public Game() { System.out.println("Welcome to the arena!"); System.out.print("Enter your hero's name: "); hero = new Character(scanner.nextLine(), rand); } public void clash() { System.out.printf("%s took a cheapshot!%n", enemy); System.out.println("(Crowd gasps)"); System.out.printf("But %s blocked it in the nick of time!%n", hero); System.out.println("(Crowd cheers)"); } public Optional<Character> rollInitiative() { int hero_initiative = rand.nextInt(1, 7), enemy_initiative = rand.nextInt(1, 7); if (hero_initiative > enemy_initiative) return Optional.of(hero); if (hero_initiative < enemy_initiative) return Optional.of(enemy); return Optional.empty(); } public void attack(Character attacker, Character defender) { Hit hit = defender.receiveHit(attacker); if (hit.blocked) { System.out.printf("%s blocks %s's attack and takes %s!%n", defender, attacker, hit); System.out.println("(Crowd cheers)"); } else { System.out.printf("%s strikes %s for %s!%n", attacker, defender, hit); System.out.println("(Crowd boos)"); } } public void doBattle() { Optional<Character> goesFirst = rollInitiative(); if (goesFirst.isPresent()) { Character attacker = goesFirst.get(); System.out.printf("%s takes initiative!%n", attacker); Character defender = hero == attacker? enemy: hero; attack(attacker, defender); } else { clash(); } } public void run() { System.out.printf("Avast, %s! Go forth!%n", hero); do { System.out.println(); hero.printStats(); enemy.printStats(); if (!hero.isAlive()) { System.out.printf("%s defeated %s!%n", enemy, hero); System.out.println("(Crowd boos aggressively)"); System.out.println("Someone from the crowd yelled \"YOU SUCK!\""); break; } if (!enemy.isAlive()) { System.out.printf("%s utterly smote %s!%n", hero, enemy); System.out.println("(Crowd ROARS)"); break; } } while (doRound()); } private boolean doRound() { while (true) { System.out.print("Enter option (1 to battle, 2 to escape)! "); switch (scanner.nextLine()) { case "1": doBattle(); return true; case "2": System.out.println("YOU COWARD!"); return false; default: System.err.println("Invalid option"); } } } public static void main(String[] args) { new Game().run(); } } 

Output

Welcome to the arena! Enter your hero's name: Julio Avast, Julio! Go forth! Julio's Stats: --------------- Health: 69 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 99 Defense: 32 Strength: 85 Enter option (1 to battle, 2 to escape)! 1 Spock takes initiative! Spock strikes Julio for 26 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 99 Defense: 32 Strength: 85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 69 Defense: 32 Strength: 85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 39 Defense: 32 Strength: 85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 9 Defense: 32 Strength: 85 Enter option (1 to battle, 2 to escape)! 1 Julio takes initiative! Julio strikes Spock for 30 points of damage! (Crowd boos) Julio's Stats: --------------- Health: 43 Defense: 59 Strength: 62 Spock's Stats: --------------- Health: 0 Defense: 32 Strength: 85 Julio utterly smote Spock! (Crowd ROARS)