stack.flowww stack.flowww - 7 months ago 18
Java Question

Java - Scanner class ; public boolean

I'm very new to Java. For my code, I need it to take three numbers from three different labs and average it out. The score can only be from (0-20) so I must have a while loop I believe and if/else statements. The problem with my code is that I am not sure how to fix my statements. I keep getting errors such as "illegal start to a statement" and "; expected" "not a statement" and "reached end of file while parsing"...Can you please help with the errors?

// This program prompts for information about grades and
// computes the lab average between three different labs

import java.util.*; // for Scanner

public class Grade {
public boolean main(String[] args){
Scanner console = new Scanner(System.in);

double lab1 = 0.0;
double lab2 = 0.0;
double lab3 = 0.0;
double labAverage = 0.0;
double add = 0.0;

// obtain values
while (true){

lab1 = console.nextDouble();
if (checkValid (lab1)){
System.out.println("First lab (0-20): ");
break;
}
else {
return false;
}

lab2 = console.nextDouble();
if (checkValid (lab2)){
System.out.println("Second lab (0-20): ");
break;
}
else {
return false;
}

lab3 = console.nextDouble();
if (checkValid (lab3)){
System.out.println("Third lab (0-20): ");
break;
}
else {
return false;
}

}

}//end Main

public boolean labAverage(double add); {
double add = lab1, lab2, lab3; {
if (add > 20) {
System.out.println("This input is illegal. Please retry.");
}
else {
return ("Lab average: " + add/3);
}
}
}


public boolean checkValid(double add); {
if (add < 0) {
System.out.println("This input is illegal. Please retry.");

}
else if (add > 20); {
System.out.println("This input is illegal. Please retry.");
}

} //end grade

Answer

Please check this, I have modified whole code..

import java.util.*; // for Scanner

public class Grade {
    public static void main(String[] args){
            Scanner console = new Scanner(System.in);
         double lab1 = 0.0;
         double lab2 = 0.0;
         double lab3 = 0.0;
         double labAverage = 0.0; 

        // obtain values

         System.out.println("First lab (0-20): ");
         lab1 = console.nextDouble();
        while (!checkValid(lab1)){
            System.out.println("Please enter valid number: ");
            lab1 = console.nextDouble();
        }

        System.out.println("Second lab (0-20): ");
        lab2 = console.nextDouble();
       while (!checkValid(lab2)){
        System.out.println("Please enter valid number: ");
        lab2 = console.nextDouble();
       }

       System.out.println("Third lab (0-20): ");
       lab3 = console.nextDouble();
      while (!checkValid(lab3)){
        System.out.println("Please enter valid number: ");
        lab3 = console.nextDouble();
      }

      System.out.println("Average is: "+labAverage(lab1,lab2,lab3));

}


private static String labAverage(double lab1, double lab2, double lab3) {
    double add = lab1+lab2+lab3; 
       return ("Lab average: " + add/3);
 }      



private static boolean checkValid(double lab) {
    if (lab < 0) {
        System.out.println("This input is illegal. Please retry.");
        return false;

      }   
      else if (lab > 20) {
        System.out.println("This input is illegal. Please retry.");
        return false;
      }
     return true;
}// end Main

}// end grade