(学习java)初始化变量报告它没有初始化

时间:2016-06-03 01:57:54

标签: java

无法找到我的代码有什么问题。这是唯一返回错误的部分 “错误:变量assignmentTotal可能尚未初始化”。 和 “错误:变量assignmentMaxTotal可能尚未初始化” 任何见解都会很棒!

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 个答案:

答案 0 :(得分:1)

您尝试在为变量分配任何值之前使用变量,因此将它们指定为

int assignmentMaxTotal = 0;
int assignmentTotal = 0;

这段代码也错了

assignmentMaxTotal =+ assMax;
assignmentTotal =+ assScore;

替换为

assignmentMaxTotal += assMax;
assignmentTotal += assScore;