如何找到:
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter Number of Students : ");
byte a=input.nextByte();
System.out.print("Enter Number of Subjects : ");
byte b=input.nextByte();
System.out.println();
byte[][] st=new byte[a][b];
//Input Marks for Subjects for different Students
for(byte i=0;i<a;i++){
System.out.println("Enter Subject Marks for Student "+(i+1)+" :- ");
for(byte x=0;x<b;x++){
System.out.print("\tEnter Marks % for Subject "+(x+1)+" : ");
st[i][x]=input.nextByte();
}
System.out.println();
}
//Find Student Total
int[] studTotal=new int[a];
for(byte i=0;i<a;i++){
for(byte x=0;x<b;x++){
studTotal[i]=studTotal[i]+st[i][x];
}
}
//Find Student Average
float[] studAvg=new float[a];
for(byte i=0;i<a;i++){
for(byte x=0;x<b;x++){
studAvg[i]=(float)studTotal[i]/b;
studAvg[i]=Math.round(studAvg[i]*100)/(float)100;
}
}
//Find Student Grade
char[] studGrade=new char[a];
for(byte i=0;i<a;i++){
if(studAvg[i]>=75){
studGrade[i]='A';
}else if(studAvg[i]>=65){
studGrade[i]='B';
}else if(studAvg[i]>=55){
studGrade[i]='C';
}else if(studAvg[i]>=35){
studGrade[i]='D';
}else{
studGrade[i]='F';
}
}
//Find Student Maximum
byte[] studMax=new byte[a];
for(byte i=0;i<a;i++){
studMax[i]=st[i][0];//<<==Problem-Solved==>>
for(byte x=0;x<b;x++){
if(st[i][x]>studMax[i]){
studMax[i]=st[i][x];
}
}
}
//Find Student Minimum
byte[] studMin=new byte[a];
for(byte i=0;i<a;i++){
studMin[i]=st[i][0];//<<==Problem-Solved==>>
for(byte x=0;x<b;x++){
if(st[i][x]<studMin[i]){
studMin[i]=st[i][x];
}
}
}
//Display Information in a Table
for(byte i=0;i<a;i++){
for(byte x=0;x<b;x++){
System.out.print(st[i][x]+"\t");
}
//Display Student's Total, Average, Grade, Maximum, Minimum
System.out.print(studTotal[i]);
System.out.print("\t"+studAvg[i]);
System.out.print("\t"+studGrade[i]);
System.out.print("\t"+studMax[i]);
System.out.print("\t"+studMin[i]);
System.out.println();
}
}
}
答案 0 :(得分:2)
因为这听起来很像家庭作业,所以我不会详细介绍,但这就是我要做的事情:
Math.max(double a, double b)
和Math.min(double a, double b)
应该会派上用场。编辑:根据您的评论,经过仔细检查,您似乎在整个应用程序中使用byte
。对于此类问题,这不是合适的数据类型。我建议您使用double
并使用nextDouble()
代替nextByte()
更改用户获取数据的方式。
答案 1 :(得分:0)
public static void main(String[] args) {
int[][] matrix = { { 2, 1, 4, 5 }, { 3, 9, 8, 43 } };
for (int i = 0; i < matrix.length; i++) {
int minInRow = matrix[i][0];
int maxInRow = matrix[i][0];
for (int j = 0; j < matrix[i].length; j++) {
if (minInRow > matrix[i][j]) {
minInRow = matrix[i][j];
}
if (maxInRow < matrix[i][j]) {
maxInRow = matrix[i][j];
}
}
System.out.println("Maximum in row is" + maxInRow);
System.out.println("Minimum in row is " + minInRow);
minInRow = matrix[i][0];
maxInRow = matrix[i][0];
}
}
上面的代码只会连续找到给定的2d数组的最小值和最大值。尝试为列编写类似的代码。