还有一个错误也表示错误:';'预期。这是代码。我检查了格式,如果我遗漏了什么,但没有。 Void属于主类以外的类。
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);
}
最多购买5然后当我运行它时说:
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 :(得分:3)
在另一种方法中声明方法是违法的。
所以不是以下......
public static void main(String[] args){
// code
}
public foo(){
// code
}
做...
PdfTable
答案 1 :(得分:2)
您的方法chassisAutomobile
,void buy1(){}
,void buy2(){}
....应该在主要
void buy3(){}