找不到符号变量错误

时间:2013-12-05 01:57:48

标签: java

所以我正在为一个编程类的作业开发一个程序。基本上我认为能够输入字母等级和课程时间,然后转换为GPA格式的成绩值。然后将这些成绩值乘以课程小时数,得出整体课程价值。然后添加所有课程值以给出总值,并且添加所有课程时间以给出总小时数。最后,通过将totalValue /总小时数除以总体GPA。

现在显然我做错了,因为当我尝试编译下面的代码墙时,我得到的10个错误都非常相似。我得到了所有cValues(cVal1,cVal2等)的“找不到符号变量”错误以及所有cHours,我不知道为什么,因为我甚至宣称它们是公开的。我甚至尝试删除它们之前的“静态”,将它们声明为公共,更改参数变量并且我很难过。这是代码:

import java.util.*;

public class stlab09
{       

public static void main (String args[])                             
{   
    System.out.println("\nLAB09 90 POINT VERSION\n\n");
    enterData();
    computeGPA();
    displayData();
}

public static String lGrade1;
public static String lGrade2;
public static String lGrade3;
public static String lGrade4;
public static int cHours1; 
public static int cHours2;
public static int cHours3;
public static int cHours4;
public static String dummy;
public static double gpa;
public static int cVal1;
public static int cVal2;
public static int cVal3;
public static int cVal4;

public static void enterData()
{       
    Scanner in = new Scanner(System.in);    
    System.out.print("Enter course 1 Grade  ===>>  ");
    lGrade1 = in.nextLine();
    System.out.print("enter course 1 Hours  ===>>  ");
    cHours1 = in.nextInt(); dummy = in.nextLine();
    System.out.print("Enter course 2 Grade  ===>>  ");
    lGrade2 = in.nextLine(); 
    System.out.print("enter course 2 Hours  ===>>  ");
    cHours2 = in.nextInt(); dummy = in.nextLine();
    System.out.print("Enter course 3 Grade  ===>>  ");
    lGrade3 = in.nextLine(); 
    System.out.print("enter course 3 Hours  ===>>  ");
    cHours3 = in.nextInt(); dummy = in.nextLine();
    System.out.print("Enter course 4 Grade  ===>>  ");
    lGrade4 = in.nextLine(); 
    System.out.print("enter course 4 Hours  ===>>  ");
    cHours4 = in.nextInt(); dummy = in.nextLine();
}

public static void computeGPA()
{
    int gVal1 = Grades.gradeValue(lGrade1);
    int gVal2 = Grades.gradeValue(lGrade2);
    int gVal3 = Grades.gradeValue(lGrade3);
    int gVal4 = Grades.gradeValue(lGrade4);
    int cVal1 = Grades.courseValue(gVal1, cHours1);
    int cVal2 = Grades.courseValue(gVal2, cHours2);
    int cVal3 = Grades.courseValue(gVal3, cHours3);
    int cVal4 = Grades.courseValue(gVal4, cHours4);
}

public static void displayData()
{   
    System.out.println();
    System.out.println("Course1 Grade: " + lGrade1 +  "  Course1 Credit Hours: " + cHours1);
    System.out.println("Course2 Grade: " + lGrade2 +  "  Course2 Credit Hours: " + cHours2);
    System.out.println("Course3 Grade: " + lGrade3 +  "  Course3 Credit Hours: " + cHours3);
    System.out.println("Course4 Grade: " + lGrade4 +  "  Course4 Credit Hours: " + cHours4);
    System.out.println();
    System.out.println("Current GPA: " + gpa);
}
}

class Grades
{
public static int gradeValue(String letterGrade)
{
    char lg = letterGrade.charAt(0);
    int value = 0;
    switch(lg)
    {
        case 'A':  value = 4;  break;
        case 'B':  value = 3;  break;
        case 'C':  value = 2;  break;
        case 'D':  value = 1;  break;
        case 'F':  value = 0;  break;
    }

    return value;
}

public static int courseValue(int a, int b)
{ 
    int cValue = a * b;

}

public static void getGPA()
{
    double totalValue = cVal1 + cVal2 + cVal3 + cVal4;   ***<<<<<<<<ERROR HERE***
    double totalHours = cHours1 + cHours2 + cHours3 + cHours4;  ***<<ERROR HERE***
    double gpa = totalValue / totalHours;
}
}

3 个答案:

答案 0 :(得分:0)

public static void computeGPA()
{
int gVal1 = Grades.gradeValue(lGrade1);
int gVal2 = Grades.gradeValue(lGrade2);
int gVal3 = Grades.gradeValue(lGrade3);
int gVal4 = Grades.gradeValue(lGrade4);
int cVal1 = Grades.courseValue(gVal1, cHours1);
int cVal2 = Grades.courseValue(gVal2, cHours2);
int cVal3 = Grades.courseValue(gVal3, cHours3);
int cVal4 = Grades.courseValue(gVal4, cHours4);
}

您将cVal1等定义为局部变量,在此方法之外无法看到它们。 尝试使用类范围的变量(删除整数)。

public static void computeGPA()
{
int gVal1 = Grades.gradeValue(lGrade1);
int gVal2 = Grades.gradeValue(lGrade2);
int gVal3 = Grades.gradeValue(lGrade3);
int gVal4 = Grades.gradeValue(lGrade4);
cVal1 = Grades.courseValue(gVal1, cHours1);
cVal2 = Grades.courseValue(gVal2, cHours2);
cVal3 = Grades.courseValue(gVal3, cHours3);
cVal4 = Grades.courseValue(gVal4, cHours4);
}

答案 1 :(得分:0)

Cvals是头等舱的成员,而不是成绩。

答案 2 :(得分:0)

您的代码中存在两个主要问题:

1.您正在尝试访问成绩getGPA()中的变量,但它们属于 stlab09

2.在类 stlab09 computeGPA()中,您不需要再次声明所有这些变量:int cVal1 = ...,请摆脱开头{{1否则它将覆盖你的静态变量cVal1。