Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 如何在2d数组中找到总分和最高分_Java_Arrays_Sum_Highest - Fatal编程技术网

Java 如何在2d数组中找到总分和最高分

Java 如何在2d数组中找到总分和最高分,java,arrays,sum,highest,Java,Arrays,Sum,Highest,我有两个表格,第一个显示学生的名字,第二个显示学生的分数。每个学生有两个学期。我是这样写的: import java.util.Scanner; public class Main { public static void main (String args[]) { Scanner Sc=new Scanner(System.in); int Number,i,j,q,n; Syst

我有两个表格,第一个显示学生的名字,第二个显示学生的分数。每个学生有两个学期。我是这样写的:

import java.util.Scanner;    

public class Main    
{    
    public static void main (String args[])    
    {   
        Scanner Sc=new Scanner(System.in);   
        int Number,i,j,q,n;

        System.out.println(" how many student in your group ?");
        Number=Sc.nextInt();
        
        //create the Array
        String [] tab =new String [Number];
        for(n=0;n<Number;n++)
        {
            System.out.println(" Enter studnt name : "+(n + 1) );
            tab[n]=Sc.nextLine();
        }
         
        int [][] marks=new int [2][Number];
        for(j=0;j<Number;j++) 
        {
            for (i=0;i<1;i++)
            {
                System.out.println(" Enter the marks of the first semster : "+(j + 1));
                marks [i][j]=Sc.nextInt();
            }
            for (q=1;q<2;q++)
            {
                 System.out.println(" Enter the marks of the Second semseter  : "+(j + 1));
                 marks [q][j]=Sc.nextInt();
            }
        }    
    }
}
import java.util.Scanner;
公共班机
{    
公共静态void main(字符串参数[])
{   
扫描仪Sc=新的扫描仪(System.in);
整数,i,j,q,n;
System.out.println(“你们组有多少学生?”);
编号=Sc.nextInt();
//创建数组
字符串[]选项卡=新字符串[编号];

对于(n=0;n我花了一段时间才理解您的数据结构,我建议使用对象,例如,为学生开设一个类。该类包含有关分数和学期等的属性,这将使其更易于使用值并更易于理解

变量应该是大小写,这意味着它们应该以小写字符开头,例如
Number
应该是
Number

我假设标记更像点,如果值越高,标记越好

要获得分数的累积分数:

int totalScore = Stream.of(marks).flatMapToInt(IntStream::of).sum();
sum()
方法返回流中所有值的累积分数

为了获得一学期的最高分数:

int highestScore = Stream.of(marks[semester]).flatMapToInt(IntStream::of).max().orElseGet(0);
max()
方法返回一个
可选的
。如果流是空的(如果学生在该学期没有任何分数),它将返回0,因为
orElseGet(0)
。在所有其他情况下,它返回该学期的最高值。

在计算每个学生的最高分数和总分数之前,您需要创建两个数组来存储该数据。数组的大小将是学生人数

int[] totalMarks = new int[studentsCount];
int[] highestMark = new int[studentsCount];

逻辑 一旦你有了这些,可以在两个不同的循环或一个单独的循环中计算总分和最高分

对于任何学生
i

highestMark[i] = max(marks[0][i], marks[1][i])
totalMarks[i] = marks[0][i] + marks[1][i]
要对所有学生执行相同的过程,请使用从
i=0
studentCount
的简单for循环


改进 您的程序也可以改进。开始为变量指定有意义的名称。不要使用
NUMBER
而使用
studentCount
。即使
Sc
工作正常,
scanner
更好。另一个改进是:

for(j=0;j<studentCount;j++) 
{
    System.out.println ("For student " + (j+1) + ": ");

    System.out.println("Enter the marks of the first semster: ");
    marks [0][j]= scannner.nextInt();
        
    System.out.println("Enter the marks of the second semseter: ");
    marks [1][j]= scanner.nextInt(); 
}    

for(j=0;j不需要为每个学期输入分数的两个for循环。第一学期可以这样写:
marks[0][j]=scanner.nextInt();
。第二学期的分数也是如此。