我试图调用一个对象,但我不能因为我使用if语句来声明该对象。帮助
我正在努力让两个机器人战斗,我最容易使用对象和toString()方法显示机器人。虽然这样做之后很难让两个机器人战斗,因为我无法确定我使用的是哪个机器人,我唯一能想到的方法是制造两个新的机器人对象但是为了确定正确的机器人统计数据,我需要使用一个新对象。
这是我的代码:
import javax.swing.JOptionPane;
public class RobotDriver
{ //armor health regen damage critical missile
public static void main(String[] args)
{
Robot Annihilator = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
Robot Gladiator = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
Robot Deadshot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
Robot Tank = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);
String robot1 = "";
String robot2 = "";
String Robot1 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose First Robot: ");
String Robot2 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose Second Robot: ");
System.out.println("Robot #1: " + Robot1 + "\n\nRobot #2: " + Robot2);
if(Robot1.equals(Annihilator.getName()))
{
Robot firstRobot = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
}
if(Robot1.equals(Gladiator.getName()))
{
Robot firstRobot = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
}
if(Robot1.equals(Deadshot.getName()))
{
Robot firstRobot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
}
if(Robot1.equals(Tank.getName()))
{
Robot firstRobot = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);
}
if(Robot2.equals(Annihilator.getName()))
{
Robot secondRobot = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
}
if(Robot2.equals(Gladiator.getName()))
{
Robot secondRobot = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
}
if(Robot2.equals(Deadshot.getName()))
{
Robot secondRobot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
}
if(Robot2.equals(Tank.getName()))
{
Robot secondRobot = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);
}
boolean fight = true;
while(fight)
{
//this is where the error occurs because the objects aren't declared
fight(firstRobot, secondRobot);
}
答案 0 :(得分:1)
声明条件语句之外的变量,然后更新条件语句中的值
import javax.swing.JOptionPane;
public class RobotDriver
{ //armor health regen damage critical missile
public static void main(String[] args)
{
Robot Annihilator = new Robot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
Robot Gladiator = new Robot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
Robot Deadshot = new Robot("Deadshot", 10, 325.9, 0, 55, .80, .15);
Robot Tank = new Robot("Tank", 45, 1300, 0, 24.9, .2, 0);
String robot1 = "";
String robot2 = "";
String Robot1 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose First Robot: ");
String Robot2 = JOptionPane.showInputDialog(Annihilator + "\n" + Gladiator + "\n" + Deadshot + "\n" + Tank + "Choose Second Robot: ");
System.out.println("Robot #1: " + Robot1 + "\n\nRobot #2: " + Robot2);
firstRobot = new Robot()
if(Robot1.equals(Annihilator.getName()))
{
firstRobot.setRobot("Annihilator", 22.88, 524.4, 2.34, 53.66, .20, .5);
}
if(Robot1.equals(Gladiator.getName()))
{
firstRobot.setRobot("Gladiator", 25, 467.3, 1.23, 58, .25, .10);
}
if(Robot1.equals(Deadshot.getName()))
{
firstRobot.setRobot("Deadshot", 10, 325.9, 0, 55, .80, .15);
}
if(Robot1.equals(Tank.getName()))
{
firstRobot.setRobot("Tank", 45, 1300, 0, 24.9, .2, 0);
}
在Robot类中有一个setRobot方法