i was getting the error: undeclared variable: IO in the following java program, but the code is right according to my knowledge

134 views Asked by At
  public class CourseGrade {
    public static void main(String[] args) {
        int examWeight = 70;
        int labWeight = 20;
        int hwWeight = 10;
        double examScore;
        double labScore;
        double hwScore;
        double finalScore;

        //ask Student to input scores for exam, lab, hw
        IO.output("Enter your exam scores: ");
        examScore = IO.inputDouble();
        IO.output("Enter your lab scores: ");
        labScore = IO.inputDouble();
        IO.output("Enter your homework scores: ");
        hwScore = IO.inputDouble();

        //compute final score as the weighted sum of exam, lab, hw score
        examScore = examScore * (examWeight / 100);
        labScore = labScore * (labWeight / 100);
        hwScore = hwScore * (hwWeight / 100);

        finalScore = examScore + labScore + hwScore;

        // output the final score
        IO.outputln("Your final score is: " + finalScore);
    }
}

I was getting this error: undeclared variable:IO again and again, and this solution problem is not available on the web

I am running this code in bluej editor for java in my opinion io part is just related to the syntax

1

There are 1 answers

0
DevilsHnd - 退した On

You would need the IO class which contains several methods to run your code. The IO class might look something like this:

public class IO {
    
    private static java.util.Scanner userIN = new java.util.Scanner(System.in);
    
    public static void output(Object input) {
        System.out.print(input);
    }

    public static void outputln(Object input) {
        System.out.println(input);
    }
    
    public static void outputf(String input, Object[] varArgs) {
        System.out.printf(input, varArgs);
    }
    
    public static String inputLine() {
        return userIN.nextLine();
    }
    
    public static String inputToken() {
        return userIN.next();
    }
    
    public static int inputInt() {
        int i = userIN.nextInt();
        userIN.nextLine(); // Consume ENTER key.
        return i;
    }
    
    public static long inputLong() {
        long i = userIN.nextLong();
        userIN.nextLine(); // Consume ENTER key.
        return i;
    }
    
    public static double inputDouble() {
        double i = userIN.nextDouble();
        userIN.nextLine(); // Consume ENTER key.
        return i;
    }
    
    public static float inputFloat() {
        float i = userIN.nextFloat();
        userIN.nextLine(); // Consume ENTER key.
        return i;
    }
    
    public static void inputClose() {
        userIN.close();
    }

    // Add more methods as required....
}

Too use with your code:

public static void main(String[] args) {                                          
    int examWeight = 70;
    int labWeight = 20;
    int hwWeight = 10;
    double examScore;
    double labScore;
    double hwScore;
    double finalScore;

    //ask Student to input scores for exam, lab, hw
    IO.output("Enter your exam scores: ");
    examScore = IO.inputDouble();
    IO.output("Enter your lab scores: ");
    labScore = IO.inputDouble();
    IO.output("Enter your homework scores: ");
    hwScore = IO.inputDouble();

    //compute final score as the weighted sum of exam, lab, hw score
    examScore = examScore * (examWeight / 100d);
    labScore = labScore * (labWeight / 100d);
    hwScore = hwScore * (hwWeight / 100d);

    finalScore = examScore + labScore + hwScore;

    // output the final score
    IO.outputln("Your final score is: " + finalScore);
}