简体   繁体   English

Java尝试根据用户输入使用带有不同参数的switch语句创建对象

[英]Java trying to create an object using switch statement with different parameters based on user input

So I'm putting together a text adventure RPG using Java, and I want to use a switch statement that will take the user's next input and create an instance of my class, FTAWeapon, using different parameters depending on the user's input. 因此,我使用Java编写了一个文本冒险RPG,我想使用一个switch语句,该语句将接受用户的下一个输入,并根据用户的输入使用不同的参数来创建我的类FTAWeapon的实例。

For example, if the user inputs "1", I want to create an instance of FTAWeapon called weapon with parameters (1, 6, 1, 5, 8, .75). 例如,如果用户输入“ 1”,则我想创建一个FTAWeapon实例,该实例称为带有参数(1、6、1、5、8,.75)的武器。

But if the user instead inputs "2", I want to create an instance of FTAWeapon, still named weapon, with parameters (2, 24, 3, 7, 15, .65). 但是,如果用户改为输入“ 2”,则我想创建一个FTAWeapon实例,该实例仍名为武器,其参数为(2,24,3,7,15,.65)。

However, I get this error: 但是,我收到此错误:

error: variable weapon is already defined in method main(String[]) 错误:方法main(String [])中已经定义了可变武器

This error appears 4 times, on all of the attempts to create an instance of the object after the first one. 在第一个实例之后的所有尝试创建对象实例的尝试中,此错误都会出现4次。

Here is the code that gives me the issue: 这是给我问题的代码:

switch(kb.next())
           {
              case "1":
                 System.out.println("\n\n*.44 Magnum Revolver added to inventory*");
                 FTAWeapon weapon = new FTAWeapon(1, 6, 1, 5, 8, .75);
                 break;

              case "2":
                 System.out.println("\n\n*Assault Rifle added to inventory*");
                 FTAWeapon weapon = new FTAWeapon(2, 24, 3, 7, 15, .65);
                 break;

              case "3":
                 System.out.println("\n\n*Laser Pistol added to inventory*");
                 FTAWeapon weapon = new FTAWeapon(3, 30, 1, 2, 6, .85);
                 break;

              case "4":
                 System.out.println("\n\n*Caravan Shotgun added to inventory*");
                 FTAWeapon weapon = new FTAWeapon(4, 14, 7, 3, 20, .50);
                 break;

              case "5":
                 System.out.println("\n\n*Plasma Rifle added to inventory*");
                 FTAWeapon weapon = new FTAWeapon(5, 24, 1, 5, 10, .70);
                 break;
           }
FTAWeapon weapon = null; // Declaration outside the switch statement
switch(kb.next())
{
          case "1":
             System.out.println("\n\n*.44 Magnum Revolver added to inventory*");
             weapon = new FTAWeapon(1, 6, 1, 5, 8, .75);
             break;

          case "2":
             System.out.println("\n\n*Assault Rifle added to inventory*");
             weapon = new FTAWeapon(2, 24, 3, 7, 15, .65);
             break;

          case "3":
             System.out.println("\n\n*Laser Pistol added to inventory*");
             weapon = new FTAWeapon(3, 30, 1, 2, 6, .85);
             break;

          case "4":
             System.out.println("\n\n*Caravan Shotgun added to inventory*");
             weapon = new FTAWeapon(4, 14, 7, 3, 20, .50);
             break;

          case "5":
             System.out.println("\n\n*Plasma Rifle added to inventory*");
             weapon = new FTAWeapon(5, 24, 1, 5, 10, .70);
             break;
}

The scope of the variables in each case clause corresponds to the whole switch statement. 每个case子句中变量的范围对应于整个switch语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM