简体   繁体   中英

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.

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).

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).

However, I get this error:

error: variable weapon is already defined in method main(String[])

This error appears 4 times, on all of the attempts to create an instance of the object after the first one.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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