如何获得每列的总和

时间:2015-05-17 07:17:33

标签: java

特别是构建 每个垂直列的 &是一项荒谬的任务。在屏幕上显示。虽然,计算水平行的总数似乎永远不是一个挑战。

  

我遇到的问题有三个问题。

     
      
  1. 如何 calculate 每个垂直列的总数?

  2.   
  3. 索引id按降序打印。如何按升序打印?

  4.   
  5. 此外,在百分比列中,小数点后面的值被丢弃。如何显示?例如..如果    answer 应该是 78.25%,它表现为 78.0%

  6.         

    P.S: (小数点后2位是我的目标。)

POJO课程 - StudentsProg.java

package com.students.marks;
import java.util.Arrays;

public class StudentsProg {

    private int id = 0;
    private String name;
    private int english;
    private int german;
    private int french;
    private int arabic;
    private double percentage;
    private int total_marks;
    private int rowHighest;




    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getEnglish() {
        return english;
    }
    public void setEnglish(int english) {
        this.english = english;
    }
    public int getGerman() {
        return german;
    }
    public void setGerman(int german) {
        this.german = german;
    }
    public int getFrench() {
        return french;
    }
    public void setFrench(int french) {
        this.french = french;
    }
    public int getArabic() {
        return arabic;
    }
    public void setArabic(int arabic) {
        this.arabic = arabic;
    }
    public double getPercentage() {
        return percentage;
    }
    public void setPercentage(double percentage) {
        this.percentage = percentage;
    }
    public int getTotal_marks() {
        return total_marks;
    }
    public void setTotal_marks(int total_marks) {   
        this.total_marks = total_marks;
    }

    public int getRowHighest() {
        return rowHighest; 
      }

    public void setRowHighest(int rowHighest) {
        this.rowHighest = rowHighest;
    }


    public String toString() {
        id = id+1; 
        return (id + "\t" +name+ "\t\t" +english+ "\t" + " " +german+ "\t" + " "+ french+ "\t" + " " +arabic+ "\t" +" " +total_marks+ "\t\t" + " " +percentage+ "\t\t" +rowHighest);

    }

}

StudentsProgMain.java

import java.util.Scanner;

public class StudentsProgMain {


    @SuppressWarnings("resource")
    public static void main(String[] args) {

        int count = 0;
        StudentsProg[] stud = new StudentsProg[15]; 

        int choice=0;
        int max = 0;


        Scanner scanner = new Scanner(System.in);

        do{

            System.out.println("1: Add new Student");
            System.out.println("2: List Student");
            System.out.println("3: List Student By Name.");
            System.out.println("4: Delete Student");
            System.out.println("5: Exit");
            System.out.println("Please enter your choice \n\n");
            choice=scanner.nextInt();

        switch(choice){ 

        case 1: 
                stud[count] = new StudentsProg();
                System.out.println("Enter student name");
                stud[count].setName(scanner.next());

                System.out.println("Enter marks in English");
                stud[count].setEnglish(scanner.nextInt());

                System.out.println("Enter marks in German");
                stud[count].setGerman(scanner.nextInt());

                System.out.println("Enter marks in French");
                stud[count].setFrench(scanner.nextInt());

                System.out.println("Enter marks in Arabic");
                stud[count].setArabic(scanner.nextInt());




                count++;

                break;

        case 2: 

            System.out.println("ID\t" + "Name \t\t\t" + "English\t" + " " + "German\t"+ " " + "French\t" + " " + "Arabic\t"
                    +" "+ "Total Marks\t" + " " + "Percentage\t" + "Highest Marks(Row)\n" +
                    "------------------------------------------------------------------------"
                    + "------------------------------------------- \n ");


            for(int i=0; i<count; i++){
                if(stud[i]!=null){

                  int total_marks = stud[i].getEnglish()+stud[i].getGerman()+ stud[i].getFrench()+stud[i].getArabic();
                  stud[i].setTotal_marks(total_marks);

                  double calc_per = ((total_marks*100)/400);

                  stud[i].setPercentage(calc_per);

                  int arrayListMarks [] = {stud[i].getEnglish(), stud[i].getFrench(), stud[i].getGerman(), stud[i].getArabic()};

                  max  = arrayListMarks[0];

                        for (int j = 1; j < arrayListMarks.length; j++) {
                            if(arrayListMarks[j] >  max)
                                max = arrayListMarks[j]; }

                  stud[i].setRowHighest(max);

                  System.out.println(stud[i].toString());
                  System.out.println("\n");}

            } 

            System.out.println("--------------------------------------------------------------"
                    + "----------------------------------------------------- \n");

             System.out.println("\tTotal :"  +"\n");

             break;   


        case 3 : 

            System.out.println("Please enter your name");
            String name = scanner.next();

            System.out.println("\n" + "ID\t" + "Name \t\t\t" + "English\t" + " " + "German\t"+ " " + "French\t" + " " + "Arabic\t"
                    +" "+ "Total Marks\t" + " " + "Percentage\t" + "Highest Marks(Row)\n" +
                    "------------------------------------------------------------------------"
                    + "------------------------------------------- \n ");

            for(int i =0 ; i<count; i++){
                if(stud[i]!=null && stud[i].getName().equals(name))
                    System.out.println(stud[i].toString()); }

            System.out.println("--------------------------------------------------------------"
                    + "----------------------------------------------------- \n");

            break;


        case 4 :
            System.out.println("Please enter your name");
            String naam = scanner.next();

            for (int i = 0; i<count; i++) {
                if(stud[i]!=null && stud[i].getName()==naam)
                    stud[i]=null;
            }
            break;

        case 5: 
            System.exit(0);
            System.out.println("You have exited successfully");

        default :
            System.out.println("Invalid choice");

    }


    }while(true);
 }

}   

1 个答案:

答案 0 :(得分:1)

百分比计算的问题是代码行double calc_per = ((total_marks*100)/400);将执行整数运算并将每个中间结果截断为整数。要解决此问题,您需要在计算中的某处包含浮点数,方法是将total_marks转换为double,如下所示:

     double calc_per = ((Integer.valueOf(total_marks).doubleValue()*100)/400);    

或者像这样使用浮点常量:

     double calc_per = ((total_marks*100.0)/400);

垂直总计应该只是将行值添加到打印循环内的变量。

我不太确定你的索引顺序问题但是toString()中的代码读取id = id+1;看起来不对。每次调用toString()时,这都会增加Id。而不是那样,您的创建代码应该在创建对象之后设置id的值,如:

            stud[count] = new StudentsProg();
            // add the following line of code.
            stud[count].setId(count);
            System.out.println("Enter student name");
            stud[count].setName(scanner.next());