Did I write this correctly? I keep getting compiler errors
I'm not sure if I am writing this correctly. I checked my notes to make sure all matching characters are in use, but I keep getting errors like: ';' expected, not a statement. Did I miss something???
import java.util.Scanner;
public class EmployeeAbsences {public static void main (String [] args) {
Scanner keyboard = new Scanner (System.in);
employees = showEmployees();
totaldays = getDays(employees);
average = averageDays(employees, totaldays);
System.out.print("Your employees averaged " + average + " days absent.");
int showEmployees();
{
int employees;
System.out.print("How many employees do you have?");
employees = keyboard.next.Int();
while (employees > 0)
{
if (employees < 0) {
System.out.print("Please enter a positive number.");
}
else
{
return employees;
}
}
}
int getDays(int employees);
{
int totaldays = 0;
int days;
for (int x = 0; x <= days; x++)
{
System.out.print("How many days was Employee #" + x + " absent?");
days = keyboard.next.Int();
totaldays = days;
totaldays = totaldays += days;
}
while (days > 0)
{
if (days < 0) {
System.out.print("Please enter a positive number.");
}
else
{
return totaldays;
}
}
}
double averageDays(employees, totaldays)
{
int totaldays;
int employees;
double average;
average = totaldays/employees;
{
return average;
}
}
}
source to share
Your methods (for example showEmployees
) should not be inside the main method, and they should not have a semi-column after the declaration:
Edit
int showEmployees();
{
...
}
to
int showEmployees()
{
...
}
and move it outside of the main method.
The same applies to getDays
and averageDays
.
It also seems that your methods are missing some return statements. Each execution path must have a return statement. For example, it getDays
has a return statement only inside a while loop. There must be at least one more return statement after the while loop if the while loop is never entered.
source to share
for every method you write you have to omit ;
, for example:
int showEmployees()
{
int employees;
System.out.print("How many employees do you have?");
employees = keyboard.next.Int();
while (employees > 0)
{
if (employees < 0) {
System.out.print("Please enter a positive number.");
}
else
{
return employees;
}
}
}
source to share
- Strip semicolons from all method declarations
- Declare a type
employees
,totaldays
andaverage
at the beginning - Most functions have a return value inside de
while
, you must return something, also if it is not part of the statementwhile
. -
averageDays
the function returnsdouble
, but in its declaration it says what it returnsint
- in
getDays
you need to initialize the variabledays
source to share