2017-04-12 56 views
-2
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语句不检查比萨饼大小的字符串。如果陈述没有正确地检查字符串是否相等

我想要得到比萨的总价,但我似乎无法得到比萨尺寸的价格。

我是新手编程,任何帮助将不胜感激。 谢谢。

+3

使用'str1.equals(STR2)'如果你想比较字符串的值 – XtremeBaumer

回答

0

当比较字符串 - 使用equals方法: string1.equals(字符串2),而不是字符串1 ==字符串2