-2

Having trouble finding whats wrong with my code. This is the only section that returns the error "error: variable assignmentTotal might not have been initialized". and "error: variable assignmentMaxTotal might not have been initialized" Any insight would be great!

import java.util.*;

public class Grade
{
public static void main(String[] args)
{
  homework();
}

public static void homework()
{
int assScore;
int assMax;
int assignmentMaxTotal;
int assignmentTotal;

  Scanner console = new Scanner(System.in);

  System.out.println("Homework and Exam 1 weights?");
  System.out.print("Using weights of 50 20 30 ");
  int weights = console.nextInt();


  System.out.println("Homework:");
  System.out.print("Number of assignments? ");
  int n = console.nextInt();

  for (int x = 0; x < n; x++)
     {
     System.out.print("Assignment " + (x + 1) + " score and max? ");
     assScore = console.nextInt();
     assMax = console.nextInt();
     assignmentMaxTotal =+ assMax;
     assignmentTotal =+ assScore;
     }
  System.out.print("Sections attended? ");
  int sections = console.nextInt();
  int sectionMax = 20;
  int sectionPoints = (sections * 4);
  int maxPoints = (assignmentMaxTotal + sectionMax);
  int totalPoints = (sectionPoints + assignmentTotal);
  System.out.println("Total Points = " + totalPoints + "/" + maxPoints);
     }
     }

1 Answers1

1

You are trying to use variables before they have been assigned any value, so assigned them like

int assignmentMaxTotal = 0;
int assignmentTotal = 0;

This code is also wrong

assignmentMaxTotal =+ assMax;
assignmentTotal =+ assScore;

replace with

assignmentMaxTotal += assMax;
assignmentTotal += assScore;
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64