(JAVA)如何使此方法与枚举一起正常工作?

huangapple go评论73阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月24日 06:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64037030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定