【问题标题】:Syntax error at else, delete this tokenelse 处的语法错误,删除此标记
【发布时间】:2013-06-28 05:25:27
【问题描述】:
import java.util.Scanner;
public class Menu {

    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in); 
        int choice, quantity;
        double price = 0;  
        double studentprice = 0;
        double totalamt = 0;
        String option,student;
        char option1, studentChar;

        do{

        System.out.println("menu");
        System.out.println("====");

        System.out.println("1. Hamburger");
        System.out.println("2. CheeseBurger");
        System.out.println("3. FrenchFries");
        System.out.println("4. SoftDrinks");

        System.out.print("Enter your choice (1,2,3 or 4):");
        choice = sc.nextInt();

        if(choice ==1)
            price = 1.5;
        else if (choice ==2)
            price = 2;
        else if (choice ==3)
            price = 2.4;
        else if (choice ==4)
        price= 1.95;

        System.out.print("Enter the quantity of your order:");
        quantity = sc.nextInt();

        totalamt += price * quantity ;
        sc.nextLine();


        System.out.print("Do you want to continue? (yes/no)?");
        option = sc.next();
        option1 = option.charAt(0);

        System.out.println("");

        }

        while(option1 == 'y');
        System.out.print("Are you a student? (yes/no?)");
        student = sc.next();
        studentChar = student.charAt(0);

        if(studentChar == 'y')
        studentprice = totalamt * 0.9;
        System.out.print("$ " + studentprice + " is the price you have to pay " );

        **else**(studentChar == 'n')
        studentprice = price;
        System.out.print("$ " + price + " is the price you have to pay " ); 
    }


}`        

最后一个else有错误。它在 else 处显示语法错误,请删除此标记。请帮忙!当我添加 do while 时,它​​打印为零。 -填充剂填充剂填充剂填充剂填充剂-

谢谢! :)

【问题讨论】:

  • “else”后面不能有 ()。只有在“如果”之后。我想 ** 不在您要编译的真实代码中。和“而(选项1 =='y');”对我来说似乎毫无意义(它甚至可以运行吗?)
  • 不,这些实际上是我要指出的
  • 是的,我可以运行 while(option1 == 'y') 我已经尝试删除 else 后面的 ()。每当我输入 else 时,它​​总是说这是一个语法错误

标签: java if-statement


【解决方案1】:

语法不正确。当您在if-block 下有多个语句时,您应该使用括号,否则只有第一个语句将在if-block 中处理,其他语句将不属于if-block

在这种情况下,else 语句没有if,因为if-blockstudentprice = totalamt * 0.9; 行之后完成。

if(studentChar == 'y'){
    studentprice = totalamt * 0.9;
    System.out.print("$ " + studentprice + " is the price you have to pay " );
}else(studentChar == 'n'){
    studentprice = price;
    System.out.print("$ " + price + " is the price you have to pay " ); 

}

【讨论】:

  • 你的循环只会在按下“Y”时退出,我不认为这是有意的。
  • 你可能会写,但这超出了范围,我可以看到这段代码需要进行某些其他更改。
【解决方案2】:

错误就在这里,它应该是else if

  **else**(studentChar == 'n')
studentprice = price;

应该是

if(studentChar == 'y'){
        studentprice = totalamt * 0.9;
        System.out.print("$ " + studentprice + " is the price you have to pay " );
        }
        else if(studentChar == 'n'){
        studentprice = price;
        System.out.print("$ " + price + " is the price you have to pay " ); 
        }else{


    }

【讨论】:

  • @user2530571 很高兴为您提供帮助。
【解决方案3】:
studentChar = 0;
while(studentChar  != 'y' && studentChar  != 'n')
{
    System.out.print("Are you a student? (yes/no?)");
    student = sc.next();
    studentChar = student.charAt(0);
}

    if(studentChar == 'y')
    {
        studentprice = totalamt * 0.9;
        System.out.print("$ " + studentprice + " is the price you have to pay " );
    }
    else if(studentChar == 'n')
    {
        studentprice = price;
        System.out.print("$ " + price + " is the price you have to pay " ); 
    }

【讨论】:

  • 测试一下。如果用户输入了无效的内容,例如“x”,它将继续。
  • 你在哪里改变了你的选项1...然后
【解决方案4】:

我对您的代码感到困惑。你是如何用

编译这段代码的

else(studentChar == 'n')

你不能用 else 检查条件,你必须使用 if 来检查

导入 java.util.Scanner;

公共类菜单{

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    int choice, quantity;
    double price = 0;
    double studentprice = 0;
    double totalamt = 0;
    String option,student;
    char option1, studentChar;

    do{

        System.out.println("menu");
        System.out.println("====");

        System.out.println("1. Hamburger");
        System.out.println("2. CheeseBurger");
        System.out.println("3. FrenchFries");
        System.out.println("4. SoftDrinks");

        System.out.print("Enter your choice (1,2,3 or 4):");
        choice = sc.nextInt();

        if(choice ==1)
            price = 1.5;
        else if (choice ==2)
            price = 2;
        else if (choice ==3)
            price = 2.4;
        else if (choice ==4)
            price= 1.95;

        System.out.print("Enter the quantity of your order:");
        quantity = sc.nextInt();

        totalamt += price * quantity ;
        sc.nextLine();


        System.out.print("Do you want to continue? (yes/no)?");
        option = sc.next();
        option1 = option.charAt(0);

        System.out.println("");

    }

    while(option1 == 'y');
    System.out.print("Are you a student? (yes/no?)");
    student = sc.next();
    studentChar = student.charAt(0);

    if(studentChar == 'y')
    {
        studentprice = totalamt * 0.9;
        System.out.print("$ " + studentprice + " is the price you have to pay " );
    }
   else if(studentChar == 'n')
   {
       studentprice = price;
       System.out.print("$ " + studentprice + " is the price you have to pay " );

   }
}
}

现在可以使用了

【讨论】:

    【解决方案5】:

    试试这个

    do
    {
        System.out.print("Are you a student? (yes/no?)");
        student = sc.next();
        studentChar = student.charAt(0);
    } while(studentChar != 'y' && studentChar != 'n');
    
    if(studentChar == 'y')
    {
      //do something here
     }
     else if(studentChar == 'n')
     {
      //other statement
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 2014-12-03
      • 1970-01-01
      相关资源
      最近更新 更多