Reputation: 3
enum ChineseMenu {
SOUP_CHICKEN(22), SOUP_VEG(32),
NOODLES_NONVEG(23), NOODLES_VEG(55),
RICE_NONVEG(43), RICE_VEG(66);
private int value;
ChineseMenu(int price) {
this.value = price;
}
public int getCost() {
return value;
}
}
class ChineseDemo {
public static void main(String[] args) {
ChineseMenu[] chineseArray = ChineseMenu.values();
for (ChineseMenu menu : chineseArray) {
System.out.println("The price of " + menu + " is ");//i want to add the price value
}
}
}
In the above code i want to add the prices value after "is". I even tried declaring a method and then calling the same. But it gives an error that static type cannot refer to non static variables
Upvotes: 0
Views: 45
Reputation: 15212
In the above code i want to add the prices value after "is"
Judging by the above line from your question and the fact that your print statement says The price of menu is
, it looks like you want to print the total cost of the Chinese menu :
You can create a variable called total
that holds the total price :
public static void main(String[] args) {
ChineseMenu[] chineseArray = ChineseMenu.values();
int total = 0;
for (ChineseMenu menu : chineseArray) {
total+=menu.getCost();
}
System.out.println("The price of Chinese menu is "+total);
}
Upvotes: 0
Reputation: 62864
How about this one:
System.out.println("The price of " + menu + " is " + menu.getCost());
Upvotes: 1