我正在努力使用方法printHighest
和printLowest
,我需要它来返回student score
,name
和ID
,但我不能弄清楚我能够让它工作的唯一方法只是得分,在大多数情况下,其余的程序工作得很好,我只是在努力解决这部分代码。
import java.util.*;
public class FinalprojectV2
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
int[] studentID = new int [5];
String[] studentName = new String [5];
int [] studentScore = new int [5];
for (int i = 0; i < 5; i++)
{
System.out.println("Student Number " + (i+1) + "");
System.out.println("Student ID Number: ");
studentID [i] = console.nextInt();
System.out.println(" Student Name: " + console.nextLine()+"");
studentName [i] = console.nextLine();
System.out.println("Grade: ");
studentScore [i] = console.nextInt();
}
printRoaster(studentID, studentName, studentScore);
int max = printLowest(studentScore);
System.out.println("Highest score is: "+ max);
int min = printHighest(studentScore);
System.out.println("Lowest score is: "+ min);
}
public static void printRoaster(int[] studentID, String []studentName, int [] studentScore)
{
for (int i = 0; i < 5; i++)
{
java.util.Arrays.sort(studentID);
java.util.Arrays.sort(studentName);
java.util.Arrays.sort(studentScore);
}
for(int i=0;i<5;i++)
{
System.out.println(studentID[i] + " " + studentName [i] + " " + studentScore [i] + " ");
}
}
public static int printLowest(int[] inputArray)
{
int maxValue = inputArray[0];
for(int i=1;i < inputArray.length;i++){
if(inputArray[i] > maxValue){
maxValue = inputArray[i];
}
}
return maxValue;
}
public static int printHighest(int[] inputArray){
int minValue = inputArray[0];
for(int i=1;i<inputArray.length;i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
}
答案 0 :(得分:1)
更好的解决方案是 - 创建一个ID,名称和分数的学生班。
从printHighest和printLowest方法返回Student类的相应对象。
希望你理解。class Student {
int studentID;
String studentName;
int studentScore ;
........
}
在main方法中,创建
Student[] students = new Student [5];
答案 1 :(得分:0)
import java.util.*;
import static java.lang.Integer.MIN_VALUE;
public class FinalprojectV2
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
int[] studentID = new int [5];
String[] studentName = new String [5];
int [] studentScore = new int [5];
for (int i = 0; i < 5; i++)
{
System.out.println("Student Number " + (i+1) + "");
System.out.println("Student ID Number: ");
studentID [i] = console.nextInt();
System.out.println(" Student Name: " + console.nextLine()+"");
studentName [i] = console.nextLine();
System.out.println("Grade: ");
studentScore [i] = console.nextInt();
}
printRoaster(studentID, studentName, studentScore);
// int max = printLowest(studentScore);
printLowest(studentScore,studentID);
// System.out.println("Highest score is: "+ );
int min = printHighest(studentScore);
System.out.println("Lowest score is: "+ min);
}
public static void printRoaster(int[] studentID, String []studentName, int []
studentScore)
{
for (int i = 0; i < 5; i++)
{
java.util.Arrays.sort(studentID);
java.util.Arrays.sort(studentName);
java.util.Arrays.sort(studentScore);
}
for(int i=0;i<5;i++)
{
System.out.println(studentID[i] + " " + studentName [i] + " " + studentScore [i]
+ " ");
}
}
public static void printLowest(int[] inputArray, int[] id)
{
int pos = MIN_VALUE;
int maxValue = inputArray[0];
for(int i=1;i < inputArray.length;i++){
if(inputArray[i] > maxValue){
pos = i;
maxValue = inputArray[i];
}
}
print(maxValue,id[pos]);
}
public static void print(int score, int id){
System.out.println("Highest score is: "+ score +" student ID is: "+id);
}
public static int printHighest(int[] inputArray){
int minValue = inputArray[0];
for(int i=1;i<inputArray.length;i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
}
我稍微改变了你的代码,所以你需要自己完成另一种方法。这里我只是用print分隔方法,并将id数组添加到调用printLowest mehod
中