英文:
(JAVA) How do I get this method to work with an enumeration?
问题
我的预期输出是:"5件T恤,尺码中号:$50.00"
import java.util.*;
public class OrderApp {
public enum Size {SMALL, MEDIUM, LARGE, XLARGE}
public enum Item {TSHIRT, HOODIE, SHORTS}
public static final int TSHIRT_COST = 10;
public static final int HOODIE_COST = 30;
public static final int SHORTS_COST = 15;
public static final int XLARGE_SURCHARGE = 6;
public static void main(String[] args) {
System.out.println("\n5件T恤,尺码中号:$" + getCost(Item.TSHIRT, Size.MEDIUM, 5) + ".00");
}
public static int getCost(Item i, Size s, int number) {
int totalVal = 0;
if (s == Size.MEDIUM && i == Item.TSHIRT) {
totalVal = number * TSHIRT_COST;
}
return totalVal;
}
}
我一直收到一个错误,错误消息为"OrderApp.java:16: error: cannot find symbol",针对TSHIRT和MEDIUM。有人能否给我一些关于我做错了什么的见解。
英文:
My expected output is "5 t-shirts, size medium: $50.00"
import java.util.*;
public class OrderApp {
public enum Size {SMALL, MEDIUM, LARGE, XLARGE}
public enum Item {TSHIRT, HOODIE, SHORTS}
public static final int TSHIRT_COST = 10;
public static final int HOODIE_COST = 30;
public static final int SHORTS_COST = 15;
public static final int XLARGE_SURCHARGE = 6;
public static void main(String[] args) {
System.out.println("\n5 t-shirts, size medium: $" + getCost(TSHIRT, MEDIUM, 5) + ".00");
}
public static int getCost(Item i, Size s, int number) {
Size sizeVal;
Item itemVal;
sizeVal = Size.MEDIUM;
itemVal = Item.TSHIRT;
int totalVal;
if (s == Size.MEDIUM) {
if (i == Item.TSHIRT) {
totalVal = number * TSHIRT_COST;
}
}
return totalVal;
}
}
I keep receiving an error that states "OrderApp.java:16: error: cannot find symbol" for TSHIRT and MEDIUM. Can someone please give me some insight on what I am doing wrong.
答案1
得分: 0
使用 getCost(Item.TSHIRT, Size.MEDIUM)
替代,否则编译器不知道在哪里找到该符号。
英文:
Use getCost(Item.TSHIRT,Size.MEDIUM)
instead, otherwise compiler don't know where to find the symbol.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论