2016-03-12 11 views
-2

Существует также ошибка, которая говорит об ошибке: ';' ожидается. Вот код. Я проверил формат, и если мне не хватает ничего, кроме него. Пустота находится в классе, отличном от основного класса.Java: ошибка: Незаконный старт выражения

void buy1(){ 
     System.out.println("Product Code 1: Paracetamol"); 
     System.out.println("Enter quantity: "); 
     quantity = machine1.nextInt(); 
     price = paracetamol*quantity; 
     System.out.println("Price is " +price); 
     System.out.println("Enter cash amount: "); 
     cash = machine1.nextInt(); 
     total = cash-price; 
     System.out.println("Total: "+total); 
    } 

до buy5 тогда, когда я запускаю его он говорит:

drugstore1.java:11: error: illegal start of expression 
    void buy1(){ 
    ^
drugstore1.java:11: error: ';' expected 
    void buy1(){ 
      ^

Я не знаю, что это неправильно. Помогите? :(

Вот весь код:.

import java.util.Scanner; 
import java.util.*; 

class Machine { 
    public static void main(String args[]) { 
    Machine machine1 = new Machine(System.in); 
    int medicine, medicine1, medicine2, medicine3, medicine4, medicine5, amount, change, cash, price, price2, price3, price4, price5, totalquantity, quantity, quantity2, quantity3, quantity4, quantity5, totalamount, total, total2, total3, total4, total5; 
    int paracetamol=7, biogesic=6, alaxan=13, neozep=6, amoxicilin=13; 
    String answer; 
    void buy1(){ 
     System.out.println("Product Code 1: Paracetamol"); 
     System.out.println("Enter quantity: "); 
     quantity = machine1.nextInt(); 
     price = paracetamol*quantity; 
     System.out.println("Price is " +price); 
     System.out.println("Enter cash amount: "); 
     cash = machine1.nextInt(); 
     total = cash-price; 
     System.out.println("Total: "+total); 
    } 

    void buy2(){ 
     System.out.println("Product Code 2: Biogesic"); 
     System.out.println("Enter quantity: "); 
     quantity2 = machine1.nextInt(); 
     price2 = biogesic*quantity2; 
     System.out.println("Price is " +price2); 
     System.out.println("Enter cash amount: "); 
     cash = machine1.nextInt(); 
     total2 = cash-price2; 
     System.out.println("Quantity of purchased items: " +quantity2+ " \nCash amount is " +cash+ "\nTotal amount is " +total2); 
    } 

    void buy3(){  
     System.out.println("Product Code 3: Alaxan"); 
     System.out.println("Enter quantity: "); 
     quantity3 = machine1.nextInt(); 
     price3 = alaxan*quantity3; 
     System.out.println("Price is " +price3); 
     System.out.println("Enter cash amount: "); 
     cash = machine1.nextInt(); 
     total3 = cash-price3; 
     System.out.println("Quantity of purchased items: " +quantity3+ " \nCash amount is " +cash+ "\nTotal amount is " +total3); 
    } 

    void buy4(){ 
     System.out.println("Product Code 4: Neozep"); 
     System.out.println("Enter quantity: "); 
     quantity4 = machine1.nextInt(); 
     price4 = neozep*quantity4; 
     System.out.println("Price is " +price4); 
     System.out.println("Enter cash amount: "); 
     cash = machine1.nextInt(); 
     total4 = cash-price4; 
     System.out.println("Quantity of purchased items: " +quantity4+ " \nCash amount is " +cash+ "\nTotal amount is " +total4); 
     } 

    void buy5(){ 
     System.out.println("Product Code 5: Amoxicilin"); 
     System.out.println("Enter quantity: "); 
     quantity5 = machine1.nextInt(); 
     price5 = amoxicilin*quantity5; 
     System.out.println("Price is " +price5); 
     System.out.println("Enter cash amount: "); 
     cash = machine1.nextInt(); 
     total5 = cash-price5; 
     System.out.println("Quantity of purchased items: " +quantity5+ " \nCash amount is " +cash+ "\nTotal amount is " +total5); 
    } 

     void addproduct(){ 
     System.out.println("Add product: y/n"); 
     String answer= machine1.nextLine(); 
    } 
} 
public class drugstore1 
    { 
    public static void main(String args[]) { 
    do { 
     System.out.println("Enter Product Code: "); 
     medicine = machine1.nextInt(); 
      if(medicine==1) 
       { 
        machine1.buy1(); 
        machine1.addproduct(); 
       } 
      if(medicine==2) 
       { 
        machine1.buy2(); 
        machine1.addproduct(); 
       } 
    }while(answer.equalsIgnoreCase("y")); 
    } 

} 
+0

с синтаксисом, кажется, где-то в коде. – SomeJavaGuy

+0

Вы, конечно, попытались написать метод в своем основном методе. –

+0

@KevinEsche Я понятия не имею, с какого момента он указывает. – John

ответ

3

Незаконно объявить метод в рамках другого метода

Так вместо того, чтобы следующие ...

public static void main(String[] args){ 
    public foo(){} 
} 

Do ...

public static void main(String[] args){ 
    // code 
} 

public foo(){ 
    // code 
} 
2

Yo ур методы void buy1(){}, void buy2(){}, void buy3(){} .... должна быть вне основной погрешности

class Machine { 
    void buy1(){} 
    void buy2(){} 
    void buy2(){} 
    public static void main(String args[]) { 
     Machine obj = new Machine(); 

     obj.buy1(); 
     ..... 
    } 
} 
+0

Я думаю, что исправил его ... но теперь он имеет еще 79 ошибок. Можете ли вы помочь мне создать эту программу? :( – John

+0

Уверен, разместите свои ошибки и давайте посмотрим, в чем проблема :) – Gintoki

+0

Можем ли мы поговорить где-нибудь? Skype? – John

Смежные вопросы