在else处语法错误,删除此令牌 [英] Syntax error at else, delete this token

查看:196
本文介绍了在else处语法错误,删除此令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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处声明语法错误,删除此令牌。请帮忙!当我添加do while时,它会打印为零。 -Filler Filler Filler Filler Filler Filler -

There is an error on the last else. It says syntax error at else, delete this token. Please help! When i add do while, it prints zero. -Filler Filler Filler Filler Filler Filler-

谢谢! :)

推荐答案

语法不正确。如果在 if-block 下有多个语句,则应使用括号,否则只有第一个语句将在 if-block &其他声明不会是 if-block 的一部分。

Syntax is not proper. you should use bracket when you have more than one statement under you if-block, Otherwise only first statement would be treated within if-block & other statement would not be part of if-block.

在这种情况下 else 语句没有如果 as if-block studentprice = totalamt * 0.9; 行之后完成。

And In this case else statement is there without if as if-block finished after studentprice = totalamt * 0.9; line.

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处语法错误,删除此令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆