繁体   English   中英

错误:在购买类中找不到 Main 方法

[英]Error: Main method not found in class Purchase

创建一个使用 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) ??? 帮助请在我运行这个程序时感谢.. 过程完成但我仍然有错误:在类中找不到 Main 方法购买请将主要方法定义为:public static void main(String[] args)

您可以将 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
  }
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM