Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到符号变量错误_Java - Fatal编程技术网

Java 找不到符号变量错误

Java 找不到符号变量错误,java,Java,所以我正在为一个编程类的作业编写一个程序。基本上,我假设能够输入字母分数和课程时数,然后将它们转换为GPA格式的分数值。然后将这些成绩值乘以课程时数,得出总课程值。然后将所有课程值相加,得出总值,并将所有课程时数相加,得出总时数。最后,总平均成绩除以总价值/总小时数 现在很明显我做错了什么,因为当我试图编译下面的代码墙时,我得到了10个非常相似的错误。对于所有的cValues(cVal1、cVal2等)和所有Chour,我都得到了一个“找不到符号变量”错误,我不知道为什么,因为我甚至将它们声明为

所以我正在为一个编程类的作业编写一个程序。基本上,我假设能够输入字母分数和课程时数,然后将它们转换为GPA格式的分数值。然后将这些成绩值乘以课程时数,得出总课程值。然后将所有课程值相加,得出总值,并将所有课程时数相加,得出总时数。最后,总平均成绩除以总价值/总小时数

现在很明显我做错了什么,因为当我试图编译下面的代码墙时,我得到了10个非常相似的错误。对于所有的cValues(cVal1、cVal2等)和所有Chour,我都得到了一个“找不到符号变量”错误,我不知道为什么,因为我甚至将它们声明为public。我甚至尝试过删除它们之前的“static”,将它们声明为public,更改参数变量,结果我被难倒了。代码如下:

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;
}
}
import java.util.*;
公共类stlab09
{       
公共静态void main(字符串参数[])
{   
System.out.println(“\nLAB09 90点版本\n\n”);
enterData();
computeGPA();
显示数据();
}
公共静态字符串lGrade1;
公共静态字符串lGrade2;
公共静态字符串lGrade3;
公共静态字符串lGrade4;
公共静态int cHours1;
公共静态int cHours2;
公共静态int cHours3;
公共静态int cHours4;
公共静态字符串伪码;
公共静态双gpa;
公共静态int cVal1;
公共静态int cVal2;
公共静态int cVal3;
公共静态int cVal4;
公共静态无效数据()
{       
扫描仪输入=新扫描仪(系统输入);
系统输出打印(“进入课程1年级==>>”;
lGrade1=in.nextLine();
系统输出打印(“输入课程1小时==>>”;
cHours1=in.nextInt();dummy=in.nextLine();
系统输出打印(“进入课程2年级==>>”;
lGrade2=in.nextLine();
系统输出打印(“输入课程2小时==>>”;
cHours2=in.nextInt();dummy=in.nextLine();
系统输出打印(“进入课程3年级==>>”;
lGrade3=in.nextLine();
系统输出打印(“进入课程3小时==>>”;
cHours3=in.nextLine();dummy=in.nextLine();
系统输出打印(“进入课程4年级==>>”;
lGrade4=in.nextLine();
系统输出打印(“进入课程4小时==>>”;
cHours4=in.nextLine();dummy=in.nextLine();
}
公共静态void computeGPA()
{
int gVal1=等级。等级值(lGrade1);
int gVal2=等级。等级值(lGrade2);
int gVal3=等级。等级值(lGrade3);
int gVal4=等级。等级值(lGrade4);
int cVal1=等级。课程价值(gVal1,cHours1);
int cVal2=等级。课程价值(gVal2,cHours2);
int cVal3=等级。课程价值(gVal3,cHours3);
int cVal4=等级。课程价值(gVal4,cHours4);
}
公共静态void displayData()
{   
System.out.println();
系统输出打印项次(“课程1等级:+lGrade1+”课程1学分:+cHours1);
系统输出打印项次(“课程2等级:+lGrade2+”课程2学分:+cHours2);
系统输出打印项次(“课程3年级:+lGrade3+”课程3学分:+cHours3);
系统输出打印项次(“课程4年级:+lGrade4+”课程4学时:+cHours4);
System.out.println();
System.out.println(“当前GPA:+GPA”);
}
}
班级成绩
{
公共静态整型等级值(字符串letterGrade)
{
字符lg=字母等级。字符(0);
int值=0;
开关(lg)
{
案例“A”:值=4;中断;
案例“B”:值=3;中断;
案例“C”:值=2;中断;
案例“D”:值=1;中断;
案例“F”:值=0;中断;
}
返回值;
}
公共静态int courseValue(int a、int b)
{ 
int c值=a*b;
}
公共静态void getGPA()
{
双倍总值=cVal1+cVal2+cVal3+cVal4;***In

您将cVal1等定义为局部变量,在该方法之外无法看到它们。 尝试使用类作用域变量(删除int)


CVAL是头等舱的成员,而不是年级的成员。

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

1.您正在尝试访问类等级
getGPA()
中的那些变量,但它们属于类stlab09


2.在类stlab09的
computeGPA()
中,您不需要再次声明所有这些变量:
int-cVal1=…
,请去掉开头的
int
,否则它将覆盖静态变量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);
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 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);
}