import java.util.Scanner;
public class Pizza {
public String sizeOfPizza;
public int numToppings;
public Pizza (){
}
public void setValues (String size, int toppings){
sizeOfPizza = size;
numToppings = toppings;
}
public double getSizePrice(){
double price = 0;
if (sizeOfPizza == "Large"){
price = 14;
}
else if (sizeOfPizza == "Medium"){
price = 12;
}
else if (sizeOfPizza == "Small"){
price = 10;
}
return price;
}
public int getToppings(){
return numToppings;
}
public double calcCost(){
int x = getToppings();
x = x*2;
double y = getSizePrice();
double cost = x + y;
return cost;
}
public void output(){
System.out.printf("Your pizza costs $%s", calcCost());
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int toppings = 0;
System.out.println("Enter size of pizza: ");
String sizeInput = keyboard.nextLine();
System.out.println("Enter No. of cheese: ");
int toppingInput = keyboard.nextInt();
toppings += toppingInput;
System.out.println("Enter No. of pepperoni: ");
toppingInput = keyboard.nextInt();
toppings += toppingInput;
System.out.println("Enter No. of ham: ");
toppingInput = keyboard.nextInt();
toppings += toppingInput;
System.out.println(toppings);
Pizza pizzaObject = new Pizza();
pizzaObject.setValues(sizeInput, toppings);
pizzaObject.output();
}
}
如何找出披萨的总价? 由于某种原因,if语句不会检查字符串的大小。
我想得到披萨的总价,但我似乎无法得到披萨大小的价格。
我是编程新手,任何帮助都将不胜感激。 感谢。
答案 0 :(得分:0)
比较String时 - 使用方法equals: string1.equals(string2),而不是string1 == string2