错误:在类Purchase中找不到主要方法

时间:2015-12-08 15:52:13

标签: java

创建一个使用Purchase类的Java程序

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class Purchase
{
String ItemName;
double ItemPrice;
int ItemQuantity;
double ItemTotal;

public void setItemName(String newItemName)
{
    ItemName = newItemName;
}

public void TotalCost(double newItemPrice, int newItemQuantity)
{
    ItemPrice = newItemPrice;
    ItemQuantity = newItemQuantity;
}

public void readInput ()
{
    Scanner sc = new Scanner (System.in);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try
{
    System.out.print("Enter Item Name : ");
    ItemName = br.readLine();
}

catch (IOException e)
{
}
    System.out.print ("Enter Item Price & Item Quantity.");
    System.out.print ("For Example 95.50 Price 8 Quantity is entered as");
    System.out.print ("95.50 8");
    System.out.println ("Enter price of item & Quantity of Item, now:");
    ItemPrice = sc.nextDouble();
    ItemQuantity = sc.nextInt();
}

public void writeOutput ()
{
    System.out.print(ItemName + " " +ItemPrice + " " +ItemQuantity + " Total Cost " +ItemTotal);
}

    public String getItemName()
    {
        return ItemName;
    }
    public double getItemPrice()
    {
        return ItemPrice;
    }
    public int getItemQuantity()
    {
        return ItemQuantity;
    }
    public double TotalCost()
    {
        double ItemTotal = ItemPrice * ItemQuantity;
        return ItemTotal;
    }
    }

在哪里以及如何将静态void main(String [] args)??? 请帮助谢谢 当我运行这个程序..过程完成但我仍然得到错误:在类购买中找不到主要方法请将主方法定义为:public static void main(String [] args)

1 个答案:

答案 0 :(得分:1)

您可以将main方法放在类中的任何位置,而不是其他方法。例如:

public class Purchase
{
  String ItemName;
  double ItemPrice;
  int ItemQuantity;
  double ItemTotal;

  public static void main(String[] args)
  {
    Purchase purch = new Purchase();
    //further actions here
  }
...
}