I\'m getting stuck in the while loop with only alice in charge. I need all 3 of
ID: 3619470 • Letter: I
Question
I'm getting stuck in the while loop with only alice in charge. I need all 3 of them in charge.import java.util.Scanner;
public class tank
{
public int ammo = 5;
public int armor = 60;
public String name;
public boolean alive = true;
Scanner so = new Scanner(System.in);
public tank(String name)
{
this.name = name;
this.ammo = 5;
this.armor = 60;
this.alive = true;
}
public tank() {}
public int getAmmo()
{
return this.ammo;
}
public int getArmor()
{
return this.armor;
}
public String getName()
{
return this.name;
}
public void fire()
{
this.ammo -= 1;
}
public void hit()
{
this.armor -= 20;
}
public void whoFire()
{
System.out.println("Who fires?");
String s1 = so.next();
if (s1.equals(this.getName()))
{
fire();
System.out.println(s1 + " has fired.");
}
}
public void hitWho()
{
System.out.println("To hit who?");
String s2 = so.next();
if (s2.equals(this.getName()))
{
hit();
System.out.println(s2 + " has been hit.");
}
}
public void exploded()
{
if (this.getArmor() <= 0)
{
this.alive = false;
System.out.println(this.getName() + " has exploded");
}
}
public void living()
{
while(this.alive != false)
{
System.out.println(this.getName() + " has " + this.getAmmo() + " ammo " + this.getArmor() + " armor.");
whoFire();
hitWho();
exploded();
}
}
public String toString()
{
String s = this.getName() + " has " + this.getAmmo() + " ammo " + this.getArmor() + " armor.";
System.out.println(s);
return s;
}
public static void main(String[] args)
{
tank a = new tank("Alice");
tank b = new tank("Bob");
tank c = new tank("Carol");
a.living();
}
} import java.util.Scanner;
public class tank
{
public int ammo = 5;
public int armor = 60;
public String name;
public boolean alive = true;
Scanner so = new Scanner(System.in);
public tank(String name)
{
this.name = name;
this.ammo = 5;
this.armor = 60;
this.alive = true;
}
public tank() {}
public int getAmmo()
{
return this.ammo;
}
public int getArmor()
{
return this.armor;
}
public String getName()
{
return this.name;
}
public void fire()
{
this.ammo -= 1;
}
public void hit()
{
this.armor -= 20;
}
public void whoFire()
{
System.out.println("Who fires?");
String s1 = so.next();
if (s1.equals(this.getName()))
{
fire();
System.out.println(s1 + " has fired.");
}
}
public void hitWho()
{
System.out.println("To hit who?");
String s2 = so.next();
if (s2.equals(this.getName()))
{
hit();
System.out.println(s2 + " has been hit.");
}
}
public void exploded()
{
if (this.getArmor() <= 0)
{
this.alive = false;
System.out.println(this.getName() + " has exploded");
}
}
public void living()
{
while(this.alive != false)
{
System.out.println(this.getName() + " has " + this.getAmmo() + " ammo " + this.getArmor() + " armor.");
whoFire();
hitWho();
exploded();
}
}
public String toString()
{
String s = this.getName() + " has " + this.getAmmo() + " ammo " + this.getArmor() + " armor.";
System.out.println(s);
return s;
}
public static void main(String[] args)
{
tank a = new tank("Alice");
tank b = new tank("Bob");
tank c = new tank("Carol");
a.living();
}
} import java.util.Scanner;
public class tank
{
public int ammo = 5;
public int armor = 60;
public String name;
public boolean alive = true;
Scanner so = new Scanner(System.in);
public tank(String name)
{
this.name = name;
this.ammo = 5;
this.armor = 60;
this.alive = true;
}
public tank() {}
public int getAmmo()
{
return this.ammo;
}
public int getArmor()
{
return this.armor;
}
public String getName()
{
return this.name;
}
public void fire()
{
this.ammo -= 1;
}
public void hit()
{
this.armor -= 20;
}
public void whoFire()
{
System.out.println("Who fires?");
String s1 = so.next();
if (s1.equals(this.getName()))
{
fire();
System.out.println(s1 + " has fired.");
}
}
public void hitWho()
{
System.out.println("To hit who?");
String s2 = so.next();
if (s2.equals(this.getName()))
{
hit();
System.out.println(s2 + " has been hit.");
}
}
public void exploded()
{
if (this.getArmor() <= 0)
{
this.alive = false;
System.out.println(this.getName() + " has exploded");
}
}
public void living()
{
while(this.alive != false)
{
System.out.println(this.getName() + " has " + this.getAmmo() + " ammo " + this.getArmor() + " armor.");
whoFire();
hitWho();
exploded();
}
}
public String toString()
{
String s = this.getName() + " has " + this.getAmmo() + " ammo " + this.getArmor() + " armor.";
System.out.println(s);
return s;
}
public static void main(String[] args)
{
tank a = new tank("Alice");
tank b = new tank("Bob");
tank c = new tank("Carol");
a.living();
}
}
Explanation / Answer
import java.util.Scanner; public class tank { public int ammo = 5; public int armor = 60; public String name; public boolean alive = true; public static Scanner so = new Scanner(System.in); public tank(String name) { this.name = name; this.ammo = 5; this.armor = 60; this.alive = true; } public tank() {} public int getAmmo() { return this.ammo; } public int getArmor() { return this.armor; } public String getName() { return this.name; } public void fire() { this.ammo -= 1; } public void hit() { this.armor -= 20; } public void whoFire() { System.out.println("Who fires?"); String s1 = so.next(); if (s1.equals(this.getName())) { fire(); System.out.println(s1 + " has fired."); } } public void hitWho() { System.out.println("To hit who?"); String s2 = so.next(); if (s2.equals(this.getName())) { hit(); System.out.println(s2 + " has been hit."); } } public void exploded() { if (this.getArmor()Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.