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的新手,请告诉我如何不要厌恶。我在上面发布了主要方法,它没有编译。 Under是下面的圣代课程。这编译没有错误。
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
非常感谢所有帮助。给我个机会。在使用我的半破智能手机发布时,对于不稳定间距的警告。
答案 0 :(得分:1)
|| (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);
^---- ??
有一个前导(
没有尾随的`)
else
方法中的最终main
没有相应的if
...
//??
else {
newSundae.setDefault();
newSundae.Print();
}
我强烈建议您采用更合适的缩进策略和/或使用支持自动代码格式化的IDE,这将帮助您跟踪所有括号...甚至使用{{ 1}}陈述......
答案 1 :(得分:0)
从以下位置更改for循环:
for (int i - 0; i<num; i++)
到
for (int i = 0; i<num; i++)
|| (newSundae.getNumberOfScoops() = 4)
至|| (newSundae.getNumberOfScoops() == 4)
答案 2 :(得分:0)
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();
}
}
}
}