简体   繁体   English

在构造函数中声明字符串数组

[英]Declare string array in constructor

I am trying to declare an string array in the constructor but when I run the code I get an error, however the debugger doesnt point anything out, also the code works fine when not in the constructor.我试图在构造函数中声明一个字符串数组,但是当我运行代码时出现错误,但是调试器没有指出任何内容,当不在构造函数中时代码也能正常工作。 It this part:这部分:

String [][] studentdetails = new String [5][3];

Error when running code with student details array in constructor is:在构造函数中使用学生详细信息数组运行代码时出错:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: studentdetails cannot be resolved to a variable studentdetails cannot be resolved to a variable studentdetails cannot be resolved to a variable线程“main”中的异常 java.lang.Error:未解决的编译问题:studentdetails 无法解析为变量 studentdetails 无法解析为变量 studentdetails 无法解析为变量

at StudentMarksProgram.MarksInput(StudentMarksProgram.java:58) at StudentMarksProgram.(StudentMarksProgram.java:37) at StudentMarksProgram.main(StudentMarksProgram.java:119)在 StudentMarksProgram.MarksInput(StudentMarksProgram.java:58) 在 StudentMarksProgram.(StudentMarksProgram.java:37) 在 StudentMarksProgram.main(StudentMarksProgram.java:119)

import java.util.*;


public class StudentMarksProgram {
    int studentTotal;


    double [] testaverages = new double [3];

    //In this array for student details [x][0] is the student no, [x][1] is the forename of the student, [x][2] is the surname of the student
    Scanner scanner = new Scanner(System.in);

    private int [][] marks;


    private double [] studentaverages;

    public StudentMarksProgram() {
        // TODO Auto-generated constructor stub
        System.out.println("How many students are in your class?");
        studentTotal = scanner.nextInt();
        marks = new int [studentTotal][3];
        studentaverages = new double [studentTotal];

        String [][] studentdetails = new String [studentTotal][3];
        //endless for5555 loop created to run the program and any subroutines
        for (int a=0; a<=2;) {
            int input = 0;
            System.out.println("To input marks/student details press 1,");
            System.out.println("To calculate the average press 2");
            System.out.println("To display averages press 3");
            System.out.println("To search data by student number press 4");
            input = scanner.nextInt();
            switch (input) {
            case 1:
                MarksInput();
                break;
            case 2:
                AverageCalculation();
                break;
            case 3:
                AveragePrint();
                break;
            case 4:
                searchStudentNumber();
                break;
    }
    void MarksInput () {
        for (int i=0; i<studentTotal; i++) {
            System.out.println("Enter student number of student " + (i+1) + " : ");
            studentdetails[i][0] = scanner.next();
            System.out.println("Enter the first name of student " + (i+1) + " : ");
            studentdetails[i][1] = scanner.next();
            System.out.println("Enter the surname of student " + (i+1) + " : ");
            studentdetails[i][2] = scanner.next();
            for (int j=0; j<=2;) {
                System.out.println("Enter marks for student " + (i + 1) + " in assessment " + (j +1));
                marks[i][j] = scanner.nextInt();
                if ((marks[i][j] < 41) && (marks[i][j]) >= 0) {
                    j = j +1;
                } else {
                    System.out.println("Invalid marks entered - enter a value between 0 and 40 inclusive");
                }
            }
        }

modify your code like this像这样修改你的代码

String [][] studentdetails;
    public StudentMarksProgram() {

    studentdetails = new String [studentTotal][3];
}

As elliot mentioned the scope of studentdetails was inside your constructor.正如 elliot 提到的, studentdetails 的范围在您的构造函数内。 This should resolve it.这应该解决它。

You shouldn't declare your array inside the constructor.你不应该在构造函数中声明你的数组。 Try declaring it outside and then initializing it inside the constructor尝试在外部声明它,然后在构造函数内部初始化它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM