我目前刚刚开始初学java类,并且在我的代码中遇到了一个我不知道如何解决的问题。该计划背后的想法是使用主要方法之外的3种方法来获得员工数量,找出每个员工的错过日期,并平均失踪日期。
import java.util.Scanner;
public class Average
{
int daysAbsent = 0;
public static void main(String[] args)
{
int employees, daysAbsent, averageDays;
noOfEmp();
daysMissed();
avgDaysAbsent();
}
public static int noOfEmp()
{
int employees;
System.out.println("How many employees do you have? ");
Scanner keyboard = new Scanner(System.in);
employees = keyboard.nextInt();
return employees;
}
public static int daysMissed(int employees)
{
int daysAbsent, i;
Scanner keyboard = new Scanner(System.in);
for(i = 1; i == employees; i++)
{
System.out.println("How many days was Employee #" + i + " absent?");
daysAbsent = keyboard.nextInt();
}
return daysAbsent;
}
public static float avgDaysAbsent(int employees, int daysAbsent)
{
float averageDays;
averageDays = (daysAbsent/employees);
System.out.println("Your employees averaged " + averageDays + " days absent");
return averageDays;
}
}
当我尝试编译代码时,我收到了包含daysMissed()的行的错误;和avgDaysAbsent()的行;有一个错误,其中方法不能应用于给定的类型,没有找到参数,并且实际和形式参数列表的长度不同。任何帮助将不胜感激。
答案 0 :(得分:0)
您可以在没有任何参数的情况下调用方法daysMissed()
,但它需要一个int参数。
它的签名是:
public static int daysMissed(int employees)
所以你应该使用int参数调用它 - daysMissed(someIntVariable)
您与avgDaysAbsent(int employees, int daysAbsent)
有同样的错误,它需要两个整数,但您没有传递给它。
我假设你打算做的是:
public static void main(String[] args)
{
int employees, daysAbsent, averageDays;
employees = noOfEmp();
daysAbsent= daysMissed(employees);
averageDays = avgDaysAbsent(employees,daysAbsent);
}
答案 1 :(得分:0)
int employees, daysAbsent, averageDays;
employees = noOfEmp();
伊兰的纠正。
在计算平均值之前,您是否认为您必须添加输入的值?