对于循环打印无效值?

时间:2015-09-29 20:30:58

标签: java arrays

我是Java编程的新手。我开发了一个Pizza类,它接受参数并输出描述和成本。我开发了一个PizzaOrderArray类,它将比萨订单存储在一个数组中。我有一个包含main方法的类。

我正在尝试打印出订单4的费用,但内置的错误消息声称pizza_size无效并打印出值0.0

我不明白订单4的pizza_size的值是如何打印在它上面的,而pizza_size却变得无效。

我错过了什么?

Pizza.java

import java.text.NumberFormat; 
import java.util.Locale;

public class Pizza {

    public Pizza(String size, int numCheeseTop, int numPepTop, int numHamTop){
        if(!setPizzaSize(size)){
            System.out.println(size + " is invalid size." + "Use small, medium or large.");
        }
        setNumCheese(numCheeseTop);
        setNumPep(numPepTop);
        setNumHam(numHamTop);
    }

    public Pizza(String size, int numPepTop, int numHamTop){
         if(!setPizzaSize(size)){
                        System.out.println(size + " is invalid size." + "Use small, medium or large.");
                }
        pizza_cheese = 0;
        setNumPep(numPepTop);
        setNumHam(numHamTop);
    }

    public Pizza(String size, int numHamTop){
         if(!setPizzaSize(size)){
                        System.out.println(size + " is invalid size." + "Use small, medium or large.");
                }
        pizza_pep = 0;
        setNumHam(numHamTop);
        pizza_cheese = 0;
    }

    public Pizza(String size){
         if(!setPizzaSize(size)){
                        System.out.println(size + " is invalid size." + "Use small, medium or large.");
                }
        pizza_cheese = 0;
        pizza_pep = 0;
        pizza_ham = 0;
    }

    public Pizza(){
        pizza_size = "small";
        pizza_cheese = 0;
        pizza_pep = 0;
        pizza_ham = 0;
    }

    public Pizza(Pizza copyPizza)
    {
        pizza_size = copyPizza.getPizzaSize();
        pizza_cheese = copyPizza.getNumCheese();
        pizza_pep = copyPizza.getNumPep();
        pizza_ham = copyPizza.getNumHam();
    }


    //Setters

    public boolean setPizzaSize(String size){
        if(size.equalsIgnoreCase("small") || (size.equalsIgnoreCase("medium") || (size.equalsIgnoreCase("large")))){
            pizza_size = size.toLowerCase();
            return true;
        }
        return false;
    }

    public void setNumCheese(int numCheeseTop){
        pizza_cheese = numCheeseTop;
    }

    public void setNumPep(int numPepTop){
        pizza_pep = numPepTop;
    }

    public void setNumHam(int numHamTop){
        pizza_ham = numHamTop;
    }
    //End of setters

    //Getters

    public String getPizzaSize(){
        return pizza_size;
    }

    public int getNumCheese(){
        return pizza_cheese;
    }

    public int getNumPep(){
        return pizza_pep;
    }

    public int getNumHam(){
        return pizza_ham;
    }

    //End of getters

    public double calcCost(){
        if(pizza_size.toLowerCase() == "small"){
            return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "medium"){
            return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "large"){
            return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() != "small" && pizza_size.toLowerCase() != "medium" && pizza_size.toLowerCase() != "large"){
            System.out.println("Inside of calcCost if");
            System.out.println("Invalid pizza size");
            return 0;
        }
    return 0;
    }


    public String toString(){
        return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
             + pizza_ham + " ham toppings ";        }

    public String getDescription(){
        return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
             + pizza_ham + " ham toppings ";  
        }

    public String pizza_size;
    public int pizza_cheese, pizza_pep, pizza_ham;

} //End of Pizza class  

PizzaOrderArray.java

import static java.lang.System.out;

public class PizzaOrderArray{

    public String pizza_size;
    public int pizza_cheese, pizza_pep, pizza_ham; 

    private Pizza[] pizza;
    private int index = 0;


    public PizzaOrderArray(){
        index = 1;
        pizza = new Pizza[index];
    }

    public PizzaOrderArray(int i){
        index = 1;
        pizza = new Pizza[index];
    }

    public PizzaOrderArray(PizzaOrderArray poa){
        pizza = new Pizza[poa.index];
        index = poa.index;
        for (int i = 0; i < poa.index; i++){
            pizza[i] = new Pizza(poa.pizza[i]);
        }
    }

    public void setPizza(int index1, Pizza newpizza){
        pizza[index1] = new Pizza(newpizza);
    }

    public String getPizzaSize(){
               return pizza_size;
       }

       public int getNumCheese(){
               return pizza_cheese;
        }

        public int getNumPep(){
                return pizza_pep;
        }

        public int getNumHam(){
                return pizza_ham;
        }

    public String toString() {
        String s = "";
            for(int i = 0; i < index; i++) {
            s= (s + pizza[i].toString());
        }
        return s;
    }

    public double calcTotal(){
        double r = 0.0;
        for(int i = 0; i < index; i++){
            System.out.println("PizzaOrderArray-calcTotal()");
            r = r + pizza[i].calcCost();
        }
    return r;
    }

    public boolean equals(PizzaOrderArray orderarray) {
        boolean r = false;
        if(orderarray.pizza.length != pizza.length) {
            return r;
        }

        for(int i = 0; i < orderarray.pizza.length; i++) {
            if(pizza[i].equals(orderarray.pizza[i])) { 
                r = true;
            }
            else {
                return false;
            }
        }
        return r;
    }   


} //End of PizzaOrderArray class



import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.Arrays;

public class V4_Project_15_page_418 {

    public static void main (String args[]){

        //Order1
        PizzaOrderArray order1 = new PizzaOrderArray();
        Pizza pizzaone = new Pizza("Medium", 1, 1, 2);
        Pizza pizzatwo = new Pizza("Small", 1, 2, 1);
        order1.setPizza(0, pizzaone);
        System.out.println("Order 1: ");
        System.out.println(order1);
        System.out.println();

        //Order2
        Pizza pizzathree = new Pizza(pizzatwo);
        PizzaOrderArray order2 = new PizzaOrderArray(2);
        order2.setPizza(0, pizzaone);
            order2.setPizza(0, pizzatwo);
        System.out.println("Order 2: ");
        System.out.println(order2);
        System.out.println();

        //Order3
        PizzaOrderArray order3 = new PizzaOrderArray(1);
        order3.setPizza(0, pizzaone);
        order3.setPizza(0, pizzatwo);
        System.out.println("Order 3: ");
        System.out.println(order3);
        System.out.println();

        //Order4
        PizzaOrderArray order4 = new PizzaOrderArray(order1);
        System.out.println("Order4: ");
        System.out.println(order4); 
        System.out.println();

        //TEST THE PROGRAM
        System.out.println("TEST:  The total for order 4 is: " + order4.calcTotal());
        System.out.println();

        System.out.println("TEST: Is order1 equal to order2?");
        System.out.println(order1.equals(order2));
        System.out.println();

        System.out.println("TEST: Is order2 equal to order4?");
        System.out.println(order2.equals(order4));
        System.out.println();



    }//End of main class

}//End of V4_Project_15_page_418 class

2 个答案:

答案 0 :(得分:2)

您正在使用==比较字符串,它比较实例身份,而不是相等。您应该使用String.equals代替。

或者,您可以使用开关:

public double calcCost(){
  switch (pizza_size.toLowerCase()) {
    case "small":
        return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
    case "medium":
        return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
    case "large":
        return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
    default:
        System.out.println("Inside of calcCost if");
        System.out.println("Invalid pizza size");
        return 0;
    }
}

您还应该考虑使用枚举来表示披萨大小:

enum Size { SMALL, MEDIUM, LARGE }

字符串不是一个好的选择,因为你不仅可以根据不同的情况来表示一个大小(在整个代码中你有不区分大小写的比较和低层大小),但你也可以传递其他任何东西< / em>的。如果将输入限制为这几种尺寸,则代码会变得更加简单。并且,您可以使用==来比较枚举值。

答案 1 :(得分:1)

请勿使用&#39; ==&#39;测试字符串相等,而是使用equals

   public double calcCost(){
        if(pizza_size.toLowerCase() == "small"){
            return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "medium"){
            return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() == "large"){
            return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
        }
        if(pizza_size.toLowerCase() != "small" && pizza_size.toLowerCase() != "medium" && pizza_size.toLowerCase() != "large"){
            System.out.println("Inside of calcCost if");
            System.out.println("Invalid pizza size");
            return 0;
        }
        return 0;
    }

由于:

  

==测试引用相等性(它们是否是同一个对象)。

     

.equals()测试值的相等性(无论它们是否合乎逻辑   &#34;等于&#34;。)

请参阅this question以供参考。