2017-04-12 57 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