我在尝试编译文件以运行我的程序时遇到了预期的.class错误。我有一个java文件(main)调用另一个类名为Sales的java文件。
HEre是我的两个java文件的代码
import java.util.Scanner;
public class Sales
{
double product1 = 2.98;
double product2 = 4.50;
double product3 = 9.98;
double product4 = 4.49;
double product5 = 6.87;
double sale = 0;
public void printHello()
{
Scanner input = new Scanner (System.in);
System.out.print("Please enter a product number.");
int userInput = input.nextInt();
while(userInput != 0)
{
if(userInput >= 1 && userInput <= 5)
{
switch(userInput)
{
case 1: product1++;
sale += 2.98;
System.out.print("How many were sold?");
userInput = input.nextInt();
break;
case 2: product2++;
sale += 4.50;
System.out.print("How many were sold?");
userInput = input.nextInt();
break;
case 3: product3++;
sale += 9.98;
System.out.print("How many break; were sold?");
userInput = input.nextInt();
break;
case 4:product4++;
sale += 4.49;
System.out.print("How many were sold?");
userInput = input.nextInt();
break;
case 5: product5++;
sale += 6.87;
System.out.print("How many were sold?");
userInput = input.nextInt();
break;
}//end switch
} //end if
else if (userInput != 0)
System.out.println("ERROR: Incorrect Product Number, Please Try Again");
System.out.print("Please enter a Product Number");
userInput = input.nextInt();
}//end while
} //end main
public void printSales(double product1,double product2,double product3,double product4,double product5)
{
System.out.println();
System.out.printf("Product 1: $%.2f\n", product1);
System.out.printf("Product 2: $%.2f\n", product2);
System.out.printf("Product 3: $%.2f\n", product3);
System.out.printf("Product 4: $%.2f\n", product4);
System.out.printf("Product 5: $%.2f\n", product5);
System.out.printf("The total Cost is: %.2f\n", sale);
} //end printSales
} //end class
然后我的主要:
public class SalesTest
{
public static void main(String[] args)
{
Sales mySales = new Sales();
mySales.printHello();
mySales.printSales();
}//end class
}
我试图用公共类SalesTest调用公共类Sales。当我尝试编译SalesTest时,我得到:
SalesTest.java:8:error:类Sales中的printSales方法不能应用于给定类型; mySales.printSales();
必需的:双,双,双,双,双 没有发现任何争论 原因:实际和正式的参数列表长度不同
我该如何解决这个问题?
答案 0 :(得分:3)
这些方法不一样。
调用定义为:
的方法 public void printSales(double product1, double product2, double product3, double product4, double product5)
您需要使用以下内容:
mySales.printSales( 0, 0.0, 0d, 0f, Double.valueOf(0) );
(任何这些表单都可用于匹配java中方法签名中的double)
当您拨打电话时:
mySales.printSales();
你最终试图打电话给:
public void printSales()
你没有,所以你得到一个错误。
这称为方法重载。它不仅仅是必须匹配的方法的名称,而是它的签名。这包括参数。