我有逻辑错误 - 没有显示正确的结果,或者如果用户输入了错误的数据,则使用默认值

时间:2015-04-22 10:27:12

标签: java arrays

我正在编写一个包含驱动程序圣代程序和圣代课程的程序。我在编译时没有错误。

    import java.util.Scanner;

            public class SundaeDriver
            {
                public static void main(String[] args)
                {
                    Sundae newSundae = new Sundae();
                    Scanner input = new Scanner(System.in);
                    System.out.println("Which sundae flavor would you like? ");
                    newSundae.setFlavor(input.nextLine());

                    if ((newSundae.getFlavor().equalsIgnoreCase("vanilla"))||
                        (newSundae.getFlavor().equalsIgnoreCase("peanut butter"))||
                        (newSundae.getFlavor().equalsIgnoreCase("chocolate"))||
                        (newSundae.getFlavor().equalsIgnoreCase("cocoanut"))||
                        (newSundae.getFlavor().equalsIgnoreCase("cookie dough"))||
                        (newSundae.getFlavor().equalsIgnoreCase("coffee"))||
                        (newSundae.getFlavor().equalsIgnoreCase("strawberry"))||
                        (newSundae.getFlavor().equalsIgnoreCase("butter pecan")))
                        {
                        }
                        else
                        {
                            newSundae.setDefault();
                            newSundae.Print();
                        }

                    System.out.println("How many scoops?");
                    newSundae.setNumberOfScoops(input.nextInt());
                    if ((newSundae.getNumberOfScoops()==1)||
                        (newSundae.getNumberOfScoops()==2)||
                            (newSundae.getNumberOfScoops()==3)||
                                (newSundae.getNumberOfScoops()==4)||
                                    (newSundae.getNumberOfScoops()==5)||
                                        (newSundae.getNumberOfScoops()==6))
                    { 
                    }
                                    else
                                    {
            newSundae.setDefault();
            newSundae.Print();
            }
                        System.out.println("How many free toppings would you like");
                        int num = input.nextInt();
                        input.nextLine();
                        for (int i = 0; i<num;i++)
                        {
                        System.out.println("what free toppings would you like?");
                        String temp=input.nextLine();
                        if ((temp.equalsIgnoreCase("whipped cream"))||
                            (temp.equalsIgnoreCase("hot fudge syrup"))||
                            (temp.equalsIgnoreCase("multi colored sprinkles"))||
                            (temp.equalsIgnoreCase("cherry")))
                        {
                            newSundae.setStdTopping(temp);

                            }
                            else    
                            {
                        newSundae.setDefault();
                        newSundae.Print();
                        }
                   }
                  System.out.println("What free syrup would you like?");
                    newSundae.setFreeSyrupChoice(input.nextLine());
                    if ((newSundae.getFreeSyrupChoice().equalsIgnoreCase("hot fudge"))||
                        (newSundae.getFreeSyrupChoice().equalsIgnoreCase("chocolate"))||
                            (newSundae.getFreeSyrupChoice().equalsIgnoreCase("caramel"))||
                                (newSundae.getFreeSyrupChoice().equalsIgnoreCase("strawberry")))
                                {
                                }
                                else
                            {
                                newSundae.setDefault();
                                newSundae.Print();
                            }

                            System.out.println("How many deluxe toppings would you like?");
                            num = input.nextInt();
                            input.nextLine();
                            for (int i = 0; i<num; i++)
                            {

                                System.out.println("what extra toppings would you like");
                                String deluxe=input.nextLine();
                                if ((deluxe.equalsIgnoreCase("M&Ms"))||
                                    (deluxe.equalsIgnoreCase("crushed oreos"))||
                                    (deluxe.equalsIgnoreCase("reeses peices"))||
                                    (deluxe.equalsIgnoreCase("bwonie crunches"))||
                                    (deluxe.equalsIgnoreCase("mint chocolate chip"))||
                                    (deluxe.equalsIgnoreCase("marshmallows"))||
                                    (deluxe.equalsIgnoreCase("peanuts"))||
                                    (deluxe.equalsIgnoreCase("walnuts"))||
                                    (deluxe.equalsIgnoreCase("peanuts and walnuts")))
                                        {
                                            newSundae.setDeluxTopping(deluxe);
                                }


                                else
                                {
                                    newSundae.setDefault();
                                    newSundae.Print();
                                }
                            }

}
}

当用户输入正确的圣代风味,免费浇头的类型和数量以及常规浇头时,该程序应该在结束时显示总价格。但是,当用户输入所有数据时,程序才会结束。

                public class Sundae
            {
                private String flavor;
                private int numberOfScoops;
                private double costForScoops;
                private String [] stdToppingList=new String [4];
                private String freeSyrupChoice;
                private String [] deluxeToppingList= new String [9];
                private int counterD= 0;
                private double costOfDeluxeToppings;
                private double costOfSundae;
                private final double SALES_TAX= .08625;
                private double tax;
                private final double COST_PER_DELUXE_TOPPING =.75;
                private int counterFree=0;

                public Sundae()
                {
                    flavor= " vanilla ";
                    numberOfScoops=2;
                    costForScoops=2.79;
                    stdToppingList[0]=" whipped cream ";
                    stdToppingList[1]=" hot fudge syrup ";
                    stdToppingList[2]=" multi colored sprinkles ";
                    stdToppingList[3]=" cherry ";
                }
            public String getFlavor()
            {
                return flavor;
            }
            public int getNumberOfScoops()
            {
                return numberOfScoops;
            }
            public double getCostForScoops()
            {
                return costForScoops;
            }
            public String [] getStdTopping()
            {
                return stdToppingList;
            }
            public String getFreeSyrupChoice()
            {
                return freeSyrupChoice;
            }
            public String [] getDeluxeTopping()
            {
                return deluxeToppingList;

            }
            public int getCounterD()
            {
                return counterD;
            }
            public double getCostDeluxeToppings()
            {
                return costOfDeluxeToppings;

            }
            public double getCostOFSundae()
            {
                return costOfSundae;
            }

            public void setFlavor( String selection )
            {
                flavor=selection;

            }
            public void setNumberOfScoops(int number)
            {
                numberOfScoops= number;

            }
            public void setCostForScoops()
            {
            costForScoops= numberOfScoops + .79;
            }
            public void setStdTopping( String toppings )
            {
                stdToppingList[counterFree] = toppings;
                counterFree++;
            }
            public void setFreeSyrupChoice( String syrup )
            {
                freeSyrupChoice= syrup;

            }
            public void setDeluxTopping (String xtraToppings)
            {
                deluxeToppingList[counterD] = xtraToppings;
                counterD++;
            }
            public void setDefault()
            {
            flavor= " vanilla ";
            numberOfScoops= 2;
            costForScoops=2.79;
            stdToppingList[0] = " whipped cream ";
            stdToppingList[1] = " hot fudge syrup ";
            stdToppingList[2] = " multi colored sprinkles ";
            stdToppingList[3] = " cherry" ;
            }
            public void Print()
            {
            System.out.println(flavor + " "+ numberOfScoops+ " " + costForScoops +
            " " + stdToppingList[0] + " " + stdToppingList[1] + stdToppingList[2] + stdToppingList[3]);
            setCostForScoops();
            costOfDeluxeToppings = COST_PER_DELUXE_TOPPING*counterD;
            costOfSundae = costOfDeluxeToppings + costForScoops;
            tax=SALES_TAX*costOfSundae;
            System.out.printf("Subtotal : $%.2f, tax: $%.2f, grand total: $%.2f " ,
                costOfSundae, tax, (costOfSundae+tax));
            }
        }

每当用户键入非用户定义的字符串或值时,我都会收到奇怪的显示消息

                   C:\Users\DrewS>java SundaeDriver
             Which sundae flavor would you like?
                 yes//purposly inputting value, should default to defsualt flavor vanilla at 2 scoops with all 4 free toppings. 
               vanilla  2 2.79 [Ljava.lang.String;@5c647e05
                         Subtotal : $2.79, tax: $0.24, grand total: $3.03    How many scoops?
         6
        How many free toppings would you like
          3
        what free toppings would you like?
      whipped cream
            what free toppings would you like?
       hot fudge
     vanilla  2 2.79 [Ljava.lang.String;@5c647e05//weird thing keeps printing
        Subtotal : $2.79, tax: $0.24, grand total: $3.03 what free toppings  would you l
                  ke?
       hot fudge syrup
                What free syrup would you like?
              chocolate
              How many deluxe toppings would you like?
            1
       what extra toppings would you like?
   mint chocolate chip

2 个答案:

答案 0 :(得分:1)

你的“问题”(这不是一个真正的问题,我会解释)来自:

 public void Print()
 {
      System.out.println(flavor + " "+ numberOfScoops+ " " + costForScoops +
            " " +stdToppingList);

你看,当你打电话给println()时,java会尝试为每个元素调用它的方法toString()

numberofScoops.toString()工作正常,因为它是一个整数,方法toString()已经定义。

关键是,当对象的toString()方法未定义时,它会打印对象的名称和所述对象的地址,例如,如果我们采用你的{{ 1}}(请注意,它是字符串表,而不是字符串):

stdToppingList

[Ljava.lang.String;@5c647e05 是它包含的内容,您的对象是一个字符串表。 Ljava.lang.String是其哈希码。

对于“修复”,您需要逐个显示表格的所有元素:

@5c647e05

由于stdToppingList中总是有4个元素,因此可以安全地打印它们。或者,如果您要保存用户想要的浇头数量,您可以迭代它。

好的,既然我们已经修复了你的public void Print() { System.out.println(flavor + " "+ numberOfScoops+ " " + costForScoops + " " + stdToppingList[0] + " " + stdToppingList[1] + stdToppingList[2] + stdToppingList[3]); 功能,让我们深入研究(就像我经常挖掘我的圣代)主要问题,你的主要问题。你的主要是邪恶的,通过你的主要就像看到魔鬼。当我写作线条时,我向Linus祈祷,为我灵魂的圣洁拯救。

无论如何,让我们从代码中的错误开始:

  • 只要用户出错,您就可以将整个对象设置为默认值。
  • 你的if语句编写得很糟糕。你为什么不给用户选择选择什么而不是让他变成蓝色?
  • 当你要求免费打顶号码时,你不会检查用户要求的数量。我的意思是,我很乐意要求99个免费配料,根据你的程序,我可以... 但是等等!你说你的免费Toppings数量最多为4。所以是的,这是一个缺陷
  • 所有上述3分使您的代码难以阅读,难以调试,并且用户难以使用;

我建议你重构你的主人,一旦你做完就回到这里,我会等你。

让我感到饥饿的上帝。

答案 1 :(得分:0)

print()功能中

System.out.println(flavor + " "+ numberOfScoops+ " " + costForScoops +
        " " +stdToppingList);

stdToppingListString[],而不是字符串。打印一个数组会给你一个奇怪的东西,Object.toString()的javadoc所说的是

the name of the class + `@` + a hexadecimal representation of the object's hashcode

为了在Java中打印基本数组,您可以获得一个for循环并打印每个元素和一个空格,或使用类似于Arrays.toString(stdToppingList)的内容。