我该如何解决这个错误?线程“main”中的异常java.lang.NoSuchMethodError:main

时间:2022-11-07 19:26:12

I wrote this program and can get it to compile but when I go to run it I get the error.... Exception in thread "main" java.lang.NoSuchMethodError: main

我写了这个程序并且可以让它编译但是当我去运行它时我得到错误....线程“main”中的异常java.lang.NoSuchMethodError:main

does anyone have any ideas how I can fix this?

有没有人有任何想法如何解决这个问题?

public class ProductDVD
{
private String productName;
private int productID;
private long unitsInStock;
private float unitPrice;

ProductDVD()
{
    setName("UNKNOWN");
    setProductID(0);
    setUnitPrice(0);
    setUnitsInStock(0);
}

ProductDVD(String productName, int productID, float unitPrice, long unitsInStock)
{
    setName(productName);
    setProductID(productID);
    setUnitPrice(unitPrice);
    setUnitsInStock(unitsInStock);
}

public void setName( String productName )
{
   this.productName = productName;
}

public void setProductID( int productID )
{
   this.productID = productID;
}

public void setUnitsInStock( long unitsInStock )
{
   this.unitsInStock = unitsInStock;
}

public void setUnitPrice( float unitPrice )
{
   this.unitPrice = unitPrice;
}

public String getName()
{
   return productName;
}

public int getProductID()
{
   return productID;
}

public long getUnitsInStock()
{
   return unitsInStock;
}

public float getUnitPrice()
{
   return unitPrice;
}

public float getInventoryValue()
{
   return( unitsInStock * unitPrice );
}
}

public class DVDsList extends ProductDVD
{
private String director;
private float restockfee;

DVDsList()
{
    super();
    director = "Unknown";
    restockfee = 0;
}

DVDsList(String productName, int productID, float unitPrice, long unitsInStock, String director)
{
    super(productName, productID, unitPrice, unitsInStock);
    this.director = director;
}

public String getDirector()
{
    return director;
}
public float getRestockFee()
{
    return restockfee;
}
public float getInventoryValue()
{
    restockfee = (5*super.getInventoryValue())/100;

    return (super.getInventoryValue() + restockfee);
}
}

import java.util.Scanner;

public class DVDinventoryProgram
{
final int MAX = 5;
DVDsList dt[] = new DVDsList[MAX];
int numberOfDVDs = 0;

public void arraySort(int count)
{
int pass , i;
int temp;
DVDsList s = null;
if(count > 1)
{
for(pass=1; pass<count; pass++)
{
    for(i=0; i<count-pass; i++)
    {
    temp = (dt[i].getName()).compareTo(dt[i+1].getName());
       if( temp > 0)
       {
        s = dt[i];
        dt[i] = dt[i+1];
        dt[i+1] = s;
       }
    }
}
}
}

public float totalValueOfInventory()
{
float inventoryValue = 0;
for(int i=0; i<numberOfDVDs; i++)
{
    inventoryValue = inventoryValue + dt[i].getInventoryValue();
    }
return inventoryValue;
}

public void displayDVD(int index)
{
System.out.println( "Name:                   " + dt[index].getName() );
System.out.println( "ID:                     " + dt[index].getProductID() );
System.out.println( "Unit Price:             " + dt[index].getUnitPrice() );
System.out.println( "Units in Stock:         " + dt[index].getUnitsInStock() );
System.out.println( "Total Product Value:    " + dt[index].getInventoryValue() );
System.out.println( "Director:               " + dt[index].getDirector() );
System.out.println( "Restocking Fee:         " + dt[index].getRestockFee() );

System.out.println( "\nTotal Value of Inventory: " + totalValueOfInventory() );
System.out.println();
}

public static void main( String args [] )
 {
String name;
int number;
long stock;
float price;
String director;

DVDinventoryProgram dip = new DVDinventoryProgram();

Scanner input = new Scanner( System.in );

System.out.println();
System.out.println( "<<<DVD Inventory Program>>>" );
System.out.println();

DVDsList dvd;

while(true)
{
System.out.println();
System.out.print( "Enter a DVD Title(or STOP to End Program): " );
name = input.nextLine();

if(name.equalsIgnoreCase("stop"))
{
    break;
}

System.out.print( "Enter a Product ID: " );
number = input.nextInt();

System.out.print( "Enter the Unit Price: " );
price = input.nextFloat();

System.out.print( "Enter the Number of Units in Stock: " );
stock = input.nextLong();

input.nextLine();

System.out.print( "Enter the Name of the Director: ");
director = input.nextLine();

dvd = new DVDsList(name,number,price,stock,director);

if(dip.numberOfDVDs < dip.MAX)
{
    dip.dt[dip.numberOfDVDs] = dvd;
    dip.numberOfDVDs++;
    System.out.println();
    System.out.println( "<<< NEW DVD >>>" );
    dip.displayDVD(dip.numberOfDVDs - 1);
}
else
{
    System.out.println("\nArray is full. Please stop entering information.");
}

}

dip.arraySort(dip.numberOfDVDs);

System.out.println("\n<<< DVD LIST BY NAME >>>\n");
for(int i = 0; i < dip.numberOfDVDs; i++)
{
    dip.displayDVD(i);
}

System.out.println();
System.out.println( "End of Program." );
System.out.println();

}
}

1 个解决方案

#1


2  

When you call your program the class called has to have a "main" method. In your case you have the method defined in the DVDinventoryProgram class so that's the class you need to be starting with.

当你调用你的程序时,被调用的类必须有一个“主”方法。在您的情况下,您具有在DVDinventoryProgram类中定义的方法,因此您需要从该类开始。

For example:

java [ options ] class [ argument ... ]

java [options] class [参数...]

java [ options ] DVDinventoryProgram [ argument ... ]

java [options] DVDinventoryProgram [argument ...]

#1


2  

When you call your program the class called has to have a "main" method. In your case you have the method defined in the DVDinventoryProgram class so that's the class you need to be starting with.

当你调用你的程序时,被调用的类必须有一个“主”方法。在您的情况下,您具有在DVDinventoryProgram类中定义的方法,因此您需要从该类开始。

For example:

java [ options ] class [ argument ... ]

java [options] class [参数...]

java [ options ] DVDinventoryProgram [ argument ... ]

java [options] DVDinventoryProgram [argument ...]