如何计算数组元素之和

时间:2018-10-10 05:10:04

标签: java arrays multidimensional-array

我有这段代码,可以计算平均分数,但无法计算总和和百分比。

我想在学生姓名下打印学生的姓名,但我只得到学生编号。

我试图了解更多有关这些的信息,但无法通过。

你能帮我吗?

package cube;

import java.io.*;
import java.util.Scanner;

public class ReportCard {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int DB[][], nos = 0;
String arrayOfNames[] = new String[nos];
String S = "";
Scanner s = new Scanner(System.in);

void Input() throws Exception {
    System.out.print("Enter The Number Of Students : ");
    nos = Integer.parseInt(br.readLine());
    DB = new int[nos + 1][6];

    for (int i = 0; i < arrayOfNames.length; i++) {
        System.out.print("Enter the name of student:");
        arrayOfNames[i] = s.nextLine();


        System.out.print("\nEnter " + arrayOfNames[i] + "'s English   Score : ");
        DB[i][0] = Integer.parseInt(br.readLine());
        System.out.print("Enter " + arrayOfNames[i] + "'s Science Score : ");
        DB[i][1] = Integer.parseInt(br.readLine());
        System.out.print("Enter " + arrayOfNames[i] + "'s Maths  Score : ");
        DB[i][2] = Integer.parseInt(br.readLine());
        DB[i][3] = (int) (DB[i][0] + DB[i][1] + DB[i][2] / 3); //calculating the Average Marks of Each Student
        DB[i][4] = (int) (DB[i][0] + DB[i][1] + DB[i][2]);

    }
}


void PrintReport() {
    System.out.println("\nGenerated Report Card :\n\nStudent Name.  English   Science   Maths   Average   Total\n");
    for (int i = 0; i < nos; i++) {
        Padd("Student Name.  ", (i + 1));
        Padd("English   ", DB[i][0]);
        Padd("Science   ", DB[i][1]);
        Padd("Maths   ", DB[i][2]);
        Padd("Average", DB[i][3]);
        Padd("Total", DB[i][4]);
        System.out.println(S);
        S = "";
    }
}

void Padd(String S, int n) {
    int N = n, Pad = 0, size = S.length();
    while (n != 0) {
        n /= 10;
        Pad++;
    }
    System.out.print("    " + N);
    for (int i = 0; i < size - Pad - 5; i++)
        System.out.print(" ");
}

public static void main(String args[]) throws Exception {
    ReportCard obj = new ReportCard();
    obj.Input();
    obj.PrintReport();
}
}

4 个答案:

答案 0 :(得分:3)

您正在将arrayOfNames数组初始化为始终为零的长度。一旦获得变量nos的值,就应该对其进行初始化(类似于您对2d数组DB的初始化)

答案 1 :(得分:0)

您正在创建名称数组,即arrayOfNames作为长度为0的数组,因为nos最初为零。 观察一下:

int DB[][],nos=0; //nos is initialized to 0
String arrayOfNames[] = new String[nos];  //arrayOfNames is of size = nos,which is in turn equal to 0, hence arrayOfNames is basically an array which can't hold anything.

相反,请执行以下操作:仅声明arrayOfNames而不初始化它。 ==> String arrayOfNames[];

在接受大小nos后,定义字符串大小。因此应如下所示:

void Input() throws Exception {
    System.out.print("Enter The Number Of Students : ");
    nos = Integer.parseInt(br.readLine());
    arrayOfNames[] = new String[nos];  //now define the size
    ...

这将确保可以在Input()函数外部访问字符串,并且使用有效的大小定义该字符串。

答案 2 :(得分:0)

进行更正可以使您的代码运行。.

package testProgram;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class ReportCard {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int DB[][], nos = 0;
    //here initaialise reference will null
    String arrayOfNames[] = null;
    String S = "";
    Scanner s = new Scanner(System.in);

    void Input() throws Exception {
        System.out.print("Enter The Number Of Students : ");
        nos = Integer.parseInt(br.readLine());
        DB = new int[nos + 1][6];
        //create string array object here
        arrayOfNames = new String[nos];
        for (int i = 0; i < arrayOfNames.length; i++) {
            System.out.print("Enter the name of student:");
            arrayOfNames[i] = s.nextLine();

            System.out.print("\nEnter " + arrayOfNames[i] + "'s English   Score : ");
            DB[i][0] = Integer.parseInt(br.readLine());
            System.out.print("Enter " + arrayOfNames[i] + "'s Science Score : ");
            DB[i][1] = Integer.parseInt(br.readLine());
            System.out.print("Enter " + arrayOfNames[i] + "'s Maths  Score : ");
            DB[i][2] = Integer.parseInt(br.readLine());
            //take extra variable that holds total, it increases the readability of the code
            int total = DB[i][0] + DB[i][1] + DB[i][2];
            DB[i][3] = (total) / 3; //calculating the Average Marks of Each Student
            DB[i][4] = total;

        }
    }

    void PrintReport() {
        System.out.println("\nGenerated Report Card :\n\nStudent Name.  English   Science   Maths   Average   Total\n");
        for (int i = 0; i < nos; i++) {
            Padd("Student Name.  ", (i + 1));
            Padd("English   ", DB[i][0]);
            Padd("Science   ", DB[i][1]);
            Padd("Maths   ", DB[i][2]);
            Padd("Average", DB[i][3]);
            Padd("Total", DB[i][4]);
            System.out.println(S);
            S = "";
        }
    }

    void Padd(String S, int n) {
        int N = n, Pad = 0, size = S.length();
        while (n != 0) {
            n /= 10;
            Pad++;
        }
        System.out.print("    " + N);
        for (int i = 0; i < size - Pad - 5; i++)
            System.out.print(" ");
    }

    public static void main(String args[]) throws Exception {
        ReportCard obj = new ReportCard();
        obj.Input();
        obj.PrintReport();
    }
}

答案 3 :(得分:0)

数组不是动态的。您可以事先声明其大小,也可以使用Arraylist。

boolean loopNaming = true;
        int i = 0;
        //you are creating array of zero size, use ArrayList instead
        //  String[] name = new String[i];
        ArrayList<String> nameList = new ArrayList<>();

        while (loopNaming == true) {
            System.out.printf("Enter name of student or done to finish: ");
            String name = keyboard.nextLine();
            //check if name is 'done'
            if (name.equals("done")) {
                loopNaming = false;
            } else {
                nameList.add(name);
                System.out.println("Enter score: ");
                score = keyboard.nextDouble();
                //nextLine positions cursor to next line
                keyboard.nextLine();
            }
            i = i + 1;

        }
        System.out.println(nameList);