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;
    }
}

      

}

+3


source to share


4 answers


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.

+5


source


Drop the semicolon from int averageDays(employees, totaldays);

, etc.



+1


source


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;
                }
        }
    }

      

+1


source


  • Strip semicolons from all method declarations
  • Declare a type employees

    , totaldays

    and average

    at the beginning
  • Most functions have a return value inside de while

    , you must return something, also if it is not part of the statement while

    .
  • averageDays

    the function returns double

    , but in its declaration it says what it returnsint

  • in getDays

    you need to initialize the variabledays

0


source







All Articles