我的 java 程序无法编译.包括代码和错误


 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();
                            }
                        }
                      }

我之前发布了此代码,但我收到了很多负面反馈。我是Stackoverflow的新手,请告诉我如何不令人反感。我在主方法上方发布了不编译的内容。下面是下面的圣代类。这样编译时不会出错。

      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);
                            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));
                            }
                            }

我收到的错误来自。

         SundaeDriver.java

  SundaeDriver.java:69: error: ')'                expected
                            ((newSundae.getFreeSyrupChoice().equalsIgnoreCas
                       e("strawberry")))
                          ^
                                 SundaeDriver.java:70: error: ')' expected
                             {
                             ^
                    SundaeDriver.java:72: error: 'else' without 'if'
                            else
                            ^
                    SundaeDriver.java:81: error: ';' expected
                        for (int i - 0; i<num; i++)
                                  ^
                   SundaeDriver.java:81: error: not a statement
                        for (int i - 0; i<num; i++)
                                         ^
            SundaeDriver.java:81: error: ')' expected
                        for (int i - 0; i<num; i++)
                                             ^
                        SundaeDriver.java:81: error: ';' expected
                        for (int i - 0; i<num; i++)
                                                  ^
                  SundaeDriver.java:95: error: ')' expected
                                (newSundae.setDeluxTopping(deluxe);
                                                                  ^
                        SundaeDriver.java:95: error: not a statement
                                (newSundae.setDeluxTopping(deluxe);
                                ^
                SundaeDriver.java:97: error: 'else' without 'if'
                            else
                10 errors

所有的帮助将不胜感激。给我一个机会。为使用我半坏的智能手机发布时不稳定的间距道歉。

|| (newSundae.getNumberOfScoops() = 4)应该|| (newSundae.getNumberOfScoops() == 4)

    if ((newSundae.getNumberOfScoops() == 1)
                    || (newSundae.getNumberOfScoops() == 2)
                    || (newSundae.getNumberOfScoops() == 3)
                    || (newSundae.getNumberOfScoops() = 4) // <----------
                    || (newSundae.getNumberOfScoops() == 5)
                    || (newSundae.getNumberOfScoops() == 6)) {

你这里有太多的左括号...

    if ((newSundae.getFreeSyrupChoice().equalsIgnoreCase("hot fudge"))
                    || ((newSundae.getFreeSyrupChoice().equalsIgnoreCase("chocolate"))
                    || ((newSundae.getFreeSyrupChoice().equalsIgnoreCase("caramel"))
                    || ((newSundae.getFreeSyrupChoice().equalsIgnoreCase("strawberry"))) {

应该更像...

    if ((newSundae.getFreeSyrupChoice().equalsIgnoreCase("hot fudge"))
                    || (newSundae.getFreeSyrupChoice().equalsIgnoreCase("chocolate"))
                    || (newSundae.getFreeSyrupChoice().equalsIgnoreCase("caramel"))
                    || (newSundae.getFreeSyrupChoice().equalsIgnoreCase("strawberry"))) {
    }

这个for (int i -0; i < num; i++) {没有意义,可能应该for (int i = 0; i < num; i++) {

这。。。

        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);
                                              ^---- ??

具有没有尾随的 ') 的前导(

main方法中的最终else没有相应的if......

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

强烈建议您采用更合适的缩进策略和/或使用支持自动代码格式化的 IDE,这将帮助您跟踪所有括号......甚至使用switch语句...

Use it!    
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();
                                }
                            }
            }
            }

最新更新