[英]How do I avoid using if statements with a large amount of variables in java
我正在嘗試創建一個程序,要求用戶輸入大列表中6個項目的名稱。 然后,它匯總了所述項目的所有屬性,並顯示了所有屬性的總和以及成本。 我能看到的唯一方法是需要大量的if和else if語句以及for循環。 由於我需要使用的項目數量,如果if和else語句至少需要1000行,這將花費大量時間來寫出來。
這是我的代碼示例
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");
}
}
}
我上CS的第一年是一名高中生。 這可能有一個我不知道的簡單答案,所以我願意接受所有答案,即使是最明顯的答案。
重新考慮問題。
程序需要做的任務是計算用戶定義的項目集的總成本。
計算X個項目的總和的代碼是相同的。 沒有商品名稱及其成本。
因此,可以在計算邏輯之外定義數據並根據用戶輸入進行查詢。
有很多方法可以做到這一點,並且Stack Overflow並不是為您完成作業的地方。
一般而言,使用集合或自定義類存儲數據並編寫代碼以根據項目的名稱查找項目的數據。 Java有多個適合用例的本機集合。 那應該讓您開始。
創建一個具有諸如itemap,itemcdr,itemcost(及其余)之類的屬性的類。 為每個屬性創建get()和set()方法。 在實例化類時,請傳遞這些變量的值。
我本人是Garen玩家,所以我立刻意識到了這些:)
我將撇開一個事實,即您實際上從未使用過在條件中設置的變量。
據我所知,您總是為每個字符串設置相同的值。 例如:
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;
}
唯一的區別是i
和要與abyssal mask
進行比較的變量的名稱。
在這種情況下,您可以使用array重用i == 0
的代碼。 將您當前分配的值分配給數組:
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.
}
無需檢查if (i == 0)
, if (i == 1)
等,因為它正在選擇正確的值以通過items[i]
數組訪問進行比較。
這只會使您無所適從。 為了更好,您將需要某種自定義對象。
在開始定義該自定義對象之前,值得展示一下如何使用它:
// 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
:
Map<String, CustomObject> map = new HashMap<>();
map.put("abyssal mask", abyssalMask);
map.put("adaptive helm", adaptiveHelm);
// ...
然后,如上所示,您可以在程序中使用它。
這是一個可怕的, 可怕的做法。 這被稱為雙括號初始化 ,並且有很多陷阱。
更好的方法是使用帶有命名參數的語言,例如Python,您可以在其中簡單地使用所需的參數調用構造函數:
abyMask = CustomObject(health=350, mana=300, mr=55)
adapHellm = CustomObject(health=99, cdr=50)
但是,如果您陷入Java困境,那么Builder模式就可以做到:
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();
然后像以前一樣將它們放入地圖
我將邏輯分解為更簡單的東西。 使用字符串數組列表可以顯着減少if / else檢查的次數。
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
您所要做的就是使用與深淵面具相同的邏輯,對所需的其他物品使用自適應頭盔,並使用+ =的總值,您可以將其添加到先前項目的現有計算總和中。 這樣,當程序完成循環后您的冠軍擁有的物品時,您將計算出總數,最終打印將反映出您冠軍給您的所有物品所擁有的東西。 祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.