Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - Syntax error at else, delete this token

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


}`        

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-

Thankyou! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...