繁体   English   中英

如何计算数组元素的总和

[英]How to calculate sum of elements of an 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();
}
}

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

您正在创建名称数组,即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()函数外部访问该字符串,并且可以使用有效的大小对其进行定义。

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

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();
    }
}

数组不是动态的。 您可以事先声明其大小,也可以使用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);

暂无
暂无

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

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