Java 如果对象仅在if语句中声明,如何引用该对象?

Java 如果对象仅在if语句中声明,如何引用该对象?,java,object,if-statement,Java,Object,If Statement,我试图调用一个对象,但我不能,因为我正在使用if语句来声明该对象。帮忙 我正在尝试让两个机器人打架,对我来说,使用对象和toString()方法显示机器人是最简单的。但是在这样做之后,很难让两个机器人打架,因为我无法确定我使用的是哪一个机器人,我唯一能确定的方法是制作两个新的机器人对象,但为了确定正确的机器人统计数据,我需要使用一个新对象 这是我的密码: import javax.swing.JOptionPane; public class RobotDriver {

我试图调用一个对象,但我不能,因为我正在使用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);

}

在条件语句之外声明变量,然后更新条件语句中的值

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方法

您需要在
if
外部声明它,并在内部分配它。@shmosel如何在内部分配它?我的变量是私有的,我需要使用getter和setter吗?它们不是私有的,它们是本地的。很简单:
Robot-firstRobot;如果(…){firstRobot=…;}
@Enzokie我查看了另一个链接,试图找到我的问题的答案,但仍然很困惑。@Enzokie我的问题是关于非静态方法的,非静态变量是不同的。谢谢,我对代码不熟悉,所以曾经使用过setter,甚至没有考虑用一个方法设置所有变量。您仍然可以在if条件之外定义变量,并使用firstRobot=new Robot(..)进行设置-我更喜欢保持类不变。没有setRobot()方法。意思是1。在调用setRobot()之前,新的Robot()是不完整的。2.机器人可以在任何时候重新定义。您可能认为这很好,但事实并非如此。根据我的经验,使用方法设置数据非常方便。您还可以定义同一方法的不同版本,以获取不同数量的参数。祝您编码愉快^^我更改了我的问题,现在我遇到了一个新的错误,我能再次收到帮助吗?Khanh?@KhanhNghiem帮助:D