如何避免在java中使用包含大量变量的if语句

如何避免在java中使用包含大量变量的if语句,java,Java,我试图创建一个程序,要求用户输入一个大列表中6项的名称。然后,它汇总所述项目的所有属性,并显示所有属性的总和以及成本。我能看到的唯一方法是使用大量if和elseif语句以及for循环。由于我需要使用的项目的数量,它将至少有1000行if和else语句,这将花费大量的时间来写出来 这是我的代码示例 import java.util.Scanner; public class MyProgram { public static void main(String[] args) { Sca

我试图创建一个程序,要求用户输入一个大列表中6项的名称。然后,它汇总所述项目的所有属性,并显示所有属性的总和以及成本。我能看到的唯一方法是使用大量if和elseif语句以及for循环。由于我需要使用的项目的数量,它将至少有1000行if和else语句,这将花费大量的时间来写出来

这是我的代码示例

import java.util.Scanner;
public class MyProgram
{
public static void main(String[] args)
  {
    Scanner one = new Scanner(System.in);
    System.out.print("Champion: ");
    String champion = one.next();
    System.out.print("Item 1: ");
    String item1 = one.next();
    System.out.print("Item 2: ");
    String item2 = one.next();
    System.out.print("Item 3: ");
    String item3 = one.next();
    System.out.print("Item 4: ");
    String item4 = one.next();
    System.out.print("Item 5: ");
    String item5 = one.next();
    System.out.print("Item 6: ");
    String item6 = one.next();
    int totalhealth = 0;
    int totalad = 0;
    int totalap = 0;
    int totalarmor = 0;
    int totalmr = 0;
    int totalcdr = 0;
    int totalmana = 0;
    int totalcstr= 0;
    int totalls = 0;
    int totalatcksp = 0;
    int totalcost = 0;
    for(int i = 0; i < 6; i++)
    {
        int itemhealth = 0;
        int itemad = 0;
        int itemap = 0;
        int itemarmor = 0;
        int itemmr = 0;
        int itemcdr = 0;
        int itemmana = 0;
        int itemcstr = 0;
        int itemls = 0;
        int itematcksp = 0;
        int itemcost = 0;
        if(i == 0)
        {
            if(item1.equals("abyssal mask"))
        {
            itemhealth = 350;
            itemmana = 300;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2900;
        }
        else if (item1.equals("adaptive helm"))
        {
            itemhealth = 350;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2800;
        }
        else if (item1.equals("aegis of the legion"))
        {
            itemarmor = 30;
            itemmr = 30;
            itemcost = 1100;
        }
        else if (item1.equals("aether wisp"))
        {
            itemap = 30;
            itemcost = 850;
        }
        else if (item1.equals("amplifying tome"))
        {
         itemap = 20;
         itemcost = 435;
        }
        else if (item1.equals("ancient coin"))
        {
            itemcdr = 5;
            itemcost = 350;
        }
        else if (item1.equals("archangel's staff"))
        {
            itemap = 80;
            itemmana = 250;
            itemcost = 3100;
        }
        else if (item1.equals("ardent censer"))
        {
            itemap = 60;
            itemcdr = 10;
            itemcost = 2300;
        }
        else if (item1.equals("athene's unholy grail"))
        {
            itemap= 30;
            itemmr = 30;
            itemcdr = 10;
            itemcost = 2100;
        }
        else if (item1.equals("b.f.sword"))
        {
            itemad = 40;
            itemcost = 1300;
        }
        else if (item1.equals("bami's cinder"))
        {
            itemhealth = 200;
            itemcost = 900;
        }
    }
        if(i == 1)
    {
        if(item2.equals("abyssal mask"))
        {
            itemhealth = 350;
            itemmana = 300;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2900;
        }
        else if (item2.equals("adaptive helm"))
        {
            itemhealth = 350;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2800;
        }
        else if (item2.equals("aegis of the legion"))
        {
            itemarmor = 30;
            itemmr = 30;
            itemcost = 1100;
        }
        else if (item2.equals("aether wisp"))
        {
            itemap = 30;
            itemcost = 850;
        }
        else if (item2.equals("amplifying tome"))
        {
         itemap = 20;
         itemcost = 435;
        }
        else if (item2.equals("ancient coin"))
        {
            itemcdr = 5;
            itemcost = 350;
        }
        else if (item2.equals("archangel's staff"))
        {
            itemap = 80;
            itemmana = 250;
            itemcost = 3100;
        }
        else if (item2.equals("ardent censer"))
        {
            itemap = 60;
            itemcdr = 10;
            itemcost = 2300;
        }
        else if (item2.equals("athene's unholy grail"))
        {
            itemap= 30;
            itemmr = 30;
            itemcdr = 10;
            itemcost = 2100;
        }
        else if (item2.equals("b.f.sword"))
        {
            itemad = 40;
            itemcost = 1300;
        }
        else if (item2.equals("bami's cinder"))
        {
            itemhealth = 200;
            itemcost = 900;
        }

    }
    if(i == 2)
    {
        if(item3.equals("abyssal mask"))
        {
            itemhealth = 350;
            itemmana = 300;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2900;
        }
        else if (item3.equals("adaptive helm"))
        {
            itemhealth = 350;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2800;
        }
        else if (item3.equals("aegis of the legion"))
        {
            itemarmor = 30;
            itemmr = 30;
            itemcost = 1100;
        }
        else if (item3.equals("aether wisp"))
        {
            itemap = 30;
            itemcost = 850;
        }
        else if (item3.equals("amplifying tome"))
        {
         itemap = 20;
         itemcost = 435;
        }
        else if (item3.equals("ancient coin"))
        {
            itemcdr = 5;
            itemcost = 350;
        }
        else if (item3.equals("archangel's staff"))
        {
            itemap = 80;
            itemmana = 250;
            itemcost = 3100;
        }
        else if (item3.equals("ardent censer"))
        {
            itemap = 60;
            itemcdr = 10;
            itemcost = 2300;
        }
        else if (item3.equals("athene's unholy grail"))
        {
            itemap= 30;
            itemmr = 30;
            itemcdr = 10;
            itemcost = 2100;
        }
        else if (item3.equals("b.f.sword"))
        {
            itemad = 40;
            itemcost = 1300;
        }
        else if (item3.equals("bami's cinder"))
        {
            itemhealth = 200;
            itemcost = 900;
        }
    }
        if(i == 3)
    {
        if(item4.equals("abyssal mask"))
        {
            itemhealth = 350;
            itemmana = 300;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2900;
        }
        else if (item4.equals("adaptive helm"))
        {
            itemhealth = 350;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2800;
        }
        else if (item4.equals("aegis of the legion"))
        {
            itemarmor = 30;
            itemmr = 30;
            itemcost = 1100;
        }
        else if (item4.equals("aether wisp"))
        {
            itemap = 30;
            itemcost = 850;
        }
        else if (item4.equals("amplifying tome"))
        {
         itemap = 20;
         itemcost = 435;
        }
        else if (item4.equals("ancient coin"))
        {
            itemcdr = 5;
            itemcost = 350;
        }
        else if (item4.equals("archangel's staff"))
        {
            itemap = 80;
            itemmana = 250;
            itemcost = 3100;
        }
        else if (item4.equals("ardent censer"))
        {
            itemap = 60;
            itemcdr = 10;
            itemcost = 2300;
        }
        else if (item4.equals("athene's unholy grail"))
        {
            itemap= 30;
            itemmr = 30;
            itemcdr = 10;
            itemcost = 2100;
        }
        else if (item4.equals("b.f.sword"))
        {
            itemad = 40;
            itemcost = 1300;
        }
        else if (item4.equals("bami's cinder"))
        {
            itemhealth = 200;
            itemcost = 900;
        }
    }
        if(i == 4)
    {
        if(item5.equals("abyssal mask"))
        {
            itemhealth = 350;
            itemmana = 300;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2900;
        }
        else if (item5.equals("adaptive helm"))
        {
            itemhealth = 350;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2800;
        }
        else if (item5.equals("aegis of the legion"))
        {
            itemarmor = 30;
            itemmr = 30;
            itemcost = 1100;
        }
        else if (item5.equals("aether wisp"))
        {
            itemap = 30;
            itemcost = 850;
        }
        else if (item5.equals("amplifying tome"))
        {
         itemap = 20;
         itemcost = 435;
        }
        else if (item5.equals("ancient coin"))
        {
            itemcdr = 5;
            itemcost = 350;
        }
        else if (item5.equals("archangel's staff"))
        {
            itemap = 80;
            itemmana = 250;
            itemcost = 3100;
        }
        else if (item5.equals("ardent censer"))
        {
            itemap = 60;
            itemcdr = 10;
            itemcost = 2300;
        }
        else if (item5.equals("athene's unholy grail"))
        {
            itemap= 30;
            itemmr = 30;
            itemcdr = 10;
            itemcost = 2100;
        }
        else if (item5.equals("b.f.sword"))
        {
            itemad = 40;
            itemcost = 1300;
        }
        else if (item5.equals("bami's cinder"))
        {
            itemhealth = 200;
            itemcost = 900;
        }
    }
        if(i == 5)
    {
        if(item6.equals("abyssal mask"))
        {
            itemhealth = 350;
            itemmana = 300;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2900;
        }
        else if (item6.equals("adaptive helm"))
        {
            itemhealth = 350;
            itemmr = 55;
            itemcdr = 10;
            itemcost = 2800;
        }
        else if (item6.equals("aegis of the legion"))
        {
            itemarmor = 30;
            itemmr = 30;
            itemcost = 1100;
        }
        else if (item6.equals("aether wisp"))
        {
            itemap = 30;
            itemcost = 850;
        }
        else if (item6.equals("amplifying tome"))
        {
         itemap = 20;
         itemcost = 435;
        }
        else if (item6.equals("ancient coin"))
        {
            itemcdr = 5;
            itemcost = 350;
        }
        else if (item6.equals("archangel's staff"))
        {
            itemap = 80;
            itemmana = 250;
            itemcost = 3100;
        }
        else if (item6.equals("ardent censer"))
        {
            itemap = 60;
            itemcdr = 10;
            itemcost = 2300;
        }
        else if (item6.equals("athene's unholy grail"))
        {
            itemap= 30;
            itemmr = 30;
            itemcdr = 10;
            itemcost = 2100;
        }
        else if (item6.equals("b.f.sword"))
        {
            itemad = 40;
            itemcost = 1300;
        }
        else if (item6.equals("bami's cinder"))
        {
            itemhealth = 200;
            itemcost = 900;
        }
    }
System.out.println("Champion: " + champion);
System.out.println("Total health: " + totalhealth);
System.out.println("Total AD: " + totalad);
System.out.println("Total AP: " + totalap);
System.out.println("Total armor: " + totalarmor);
System.out.println("Total magic resist" + totalmr);
System.out.println("Total CDR: %" + totalcdr);
System.out.println("Total mana: " + totalmana);
System.out.println("Total Critical Strike Chance: " + totalcstr);
System.out.println("Total Life Steal: %" + totalls);
System.out.println("Total Attack Speed: " + totalatcksp);
System.out.println("Total Cost: " + totalcost + "g");


    }
  }
}
import java.util.Scanner;
公共类MyProgram
{
公共静态void main(字符串[]args)
{
扫描仪1=新扫描仪(System.in);
系统输出打印(“冠军:”);
字符串champion=one.next();
系统输出打印(“第1项:”);
String item1=one.next();
系统输出打印(“第2项:”);
String item2=one.next();
系统输出打印(“第3项:”);
String item3=one.next();
系统输出打印(“第4项:”);
String item4=one.next();
系统输出打印(“第5项:”);
String item5=one.next();
系统输出打印(“第6项:”);
String item6=one.next();
int totalhealth=0;
int totalad=0;
int totalap=0;
整型总装甲=0;
int totalmr=0;
int totalcdr=0;
智力总法力=0;
int totalcstr=0;
整数=0;
int totalatcksp=0;
整数总成本=0;
对于(int i=0;i<6;i++)
{
int itemhealth=0;
int itemad=0;
int itemap=0;
int=0;
int itemmr=0;
int itemcdr=0;
int itemmana=0;
int itemcstr=0;
int itemls=0;
int itematcksp=0;
int itemcost=0;
如果(i==0)
{
如果(第1项等于(“深海面具”))
{
itemhealth=350;
itemmana=300;
项目MR=55;
项目CDR=10;
项目成本=2900;
}
否则如果(第1项等于(“自适应舵”))
{
itemhealth=350;
项目MR=55;
项目CDR=10;
项目成本=2800;
}
否则如果(第1项等于(“军团宙斯盾”))
{
项目装甲=30;
项目Mr=30;
项目成本=1100;
}
否则,如果(第1项等于(“以太wisp”))
{
项目ap=30;
项目成本=850;
}
else if(第1项等于(“放大倍数”))
{
itemap=20;
项目成本=435;
}
如果(第1项等于(“古代硬币”))
{
项目CDR=5;
项目成本=350;
}
否则如果(第1项等于(“大天使之杖”))
{
itemap=80;
itemmana=250;
项目成本=3100;
}
else if(第1项等于(“热切香炉”))
{
itemap=60;
项目CDR=10;
项目成本=2300;
}
否则如果(第1项等于(“雅典娜的邪恶圣杯”))
{
项目ap=30;
项目Mr=30;
项目CDR=10;
项目成本=2100;
}
否则,如果(第1项等于(“b.f.Swarm”))
{
itemad=40;
项目成本=1300;
}
否则如果(第1项等于(“巴米煤渣”))
{
itemhealth=200;
项目成本=900;
}
}
如果(i==1)
{
如果(第2项等于(“深海面具”))
{
itemhealth=350;
itemmana=300;
项目MR=55;
项目CDR=10;
项目成本=2900;
}
否则如果(第2项等于(“自适应舵”))
{
itemhealth=350;
项目MR=55;
项目CDR=10;
项目成本=2800;
}
否则,如果(第2项等同(“军团之盾”))
{
项目装甲=30;
项目Mr=30;
项目成本=1100;
}
否则如果(第2项等于(“以太wisp”))
{
项目ap=30;
项目成本=850;
}
else if(第2项等于(“放大倍数”))
{
itemap=20;
项目成本=435;
}
否则,如果(第2项等于(“古钱币”))
{
项目CDR=5;
项目成本=350;
}
否则如果(第2项等于(“大天使之杖”))
{
itemap=80;
itemmana=250;
项目成本=3100;
}
else if(第2项等于(“热切香炉”))
{
itemap=60;
项目CDR=10;
项目成本=2300;
}
否则如果(第2项等于(“雅典娜的邪恶圣杯”))
{
项目ap=30;
项目Mr=30;
项目CDR=10;
项目成本=2100;
}
否则,如果(第2项等于(“b.f.Swarm”))
{
itemad=40;
项目成本=1300;
}
否则如果(第2项等于(“巴米煤渣”))
{
itemhealth=200;
项目成本=900;
}
}
如果(i==2)
{
如果(第3项等于(“深海面具”))
{
itemhealth=350;
itemmana=300;
项目MR=55;
项目CDR=10;
项目成本=2900;
}
否则如果(第3项等于(“自适应舵”))
{
itemhealth=350;
项目MR=55;
项目CDR=10;
项目成本=2800;
}
否则如果(第3项等于(“军团之盾”))
{
项目装甲=30;
项目Mr=30;
项目成本=1100;
}
否则如果(第3项等于(“以太wisp”))
{
项目ap=30;
项目成本=850;
}
else if(第3项等于(“放大倍数”))
{
itemap=20;
项目成本=435;
}
如果(第3项等于(“古钱币”))
{
项目CDR=5;
项目成本=350;
}
    if(i == 0)
    {
        if(item1.equals("abyssal mask"))
    {
        itemhealth = 350;
        itemmana = 300;
        itemmr = 55;
        itemcdr = 10;
        itemcost = 2900;
    }
    // ...

    if(i == 1)
    {
        if(item2.equals("abyssal mask"))
    {
        itemhealth = 350;
        itemmana = 300;
        itemmr = 55;
        itemcdr = 10;
        itemcost = 2900;
    }
String[] items = new String[6];
for (int i = 0; i < items.length; ++i) {
  System.out.printf("Item %d: %n", i + 1);
  items[i] = one.next();
}
if (items[i].equals("abyssal mask")) {
  itemhealth = 350;
  itemmana = 300;
  // etc.
}
// Initialize once.
Map<String, CustomObject> map = /* initialize somehow */;

// In your loop:
CustomObject obj = map.get(item[i]);
// Then use the values in obj somehow:
System.out.printf("Item %s has health %d%n", item[i], obj.health);
System.out.printf("Item %s has mana %d%n", item[i], obj.mana);
class CustomObject {
  int health;
  int mana;
  int mr;
  // etc.
}
CustomObject abyssalMask = new CustomObject{{
  health = 350;
  mana = 300;
  mr = 55;
  // etc.
}};
CustomObject adaptiveHelm = new CustomObject{{
  health = 350;
  mr = 55;
  cdr = 10;
  // etc.
}};
Map<String, CustomObject> map = new HashMap<>();
map.put("abyssal mask", abyssalMask);
map.put("adaptive helm", adaptiveHelm);
// ...
abyMask = CustomObject(health=350, mana=300, mr=55)
adapHellm = CustomObject(health=99, cdr=50)
class CustomObject {
  final int health;
  final int mana;

  private CustomObject(Builder b) {
    this.health = b.health;
    this.mana = b.mana;
  }

  static Builder builder() { return new Builder(); }

  static class Builder {
    int health;
    int mana;

    CustomObject build() { return new CustomObject(this); }

    Builder setHealth(int health) { this.health = health; return this; }
    Builder setMana(int mana) { this.mana = mana; return this; }
  }
}
CustomObject abyssalMask = CustomObject.builder().setHealth(350).setMana(300).build();
public static void main(String[] args){
ArrayList<String> allItems = new ArrayList<>();
int numberOfItems = 6;
Scanner one = new Scanner(System.in);

System.out.println("Champion: ");
String champion = one.nextLine();

for(int i = 0; i < numberOfItems; i++)
{
    System.out.println("Item " + (i + 1) + ": ");
    String item = one.nextLine();
    System.out.println("Adding item: " + item + " to index " + i + " of the list");
    allItems.add(item);
}
int totalhealth = 0;
int totalad = 0;
int totalap = 0;
int totalarmor = 0;
int totalmr = 0;
int totalcdr = 0;
int totalmana = 0;
int totalcstr= 0;
int totalls = 0;
int totalatcksp = 0;
int totalcost = 0;

for(int i = 0; i < allItems.size(); i++)
{   
    if(allItems.get(i).equals("abyssal mask"))
    {
        totalhealth += 350;
        totalmana += 300;
        totalmr += 55;
        totalcdr += 10;
        totalcost += 2900;
    }
    else if(allItems.get(i).equals("adaptive helm"))
    {
        totalhealth += 350;
        totalmr += 55;
        totalcdr += 10;
        totalcost += 2800;
    }
    //... for all the items using the += for total values
    else {
        System.out.println("Not a valid item: " + allItems.get(i));
        allItems.remove(i);
        i--;
    }
}

System.out.println("Champion: " + champion);
System.out.println("Total health: " + totalhealth);
System.out.println("Total AD: " + totalad);
System.out.println("Total AP: " + totalap);
System.out.println("Total armor: " + totalarmor);
System.out.println("Total magic resist " + totalmr);
System.out.println("Total CDR: %" + totalcdr);
System.out.println("Total mana: " + totalmana);
System.out.println("Total Critical Strike Chance: " + totalcstr);
System.out.println("Total Life Steal: %" + totalls);
System.out.println("Total Attack Speed: " + totalatcksp);
System.out.println("Total Cost: " + totalcost + "g");
}
Champion: 
alexlchico
Item 1: 
abyssal mask
Adding item: abyssal mask to index 0 of the list
Item 2: 
adaptive helm
Adding item: adaptive helm to index 1 of the list
Item 3: 
pikachu
Adding item: pikachu to index 2 of the list
Item 4: 
adaptive helm
Adding item: adaptive helm to index 3 of the list
Item 5: 
abyssal mask
Adding item: abyssal mask to index 4 of the list
Item 6: 
ball of lint
Adding item: ball of lint to index 5 of the list
Not a valid item: pikachu
Not a valid item: ball of lint

Champion: alexlchico
Total health: 1400
Total AD: 0
Total AP: 0
Total armor: 0
Total magic resist 220
Total CDR: %40
Total mana: 600
Total Critical Strike Chance: 0
Total Life Steal: %0
Total Attack Speed: 0
Total Cost: 11400g