索引匹配最大/最小分数 (Java)
Posted
技术标签:
【中文标题】索引匹配最大/最小分数 (Java)【英文标题】:Index match max/min scores (Java) 【发布时间】:2021-09-14 00:52:12 【问题描述】:我正在完成一项编码作业,并且即将完成保存我的最终打印语句,该语句需要匹配名称和最大和最小分数。
我已经能够使用两个 if 语句在两个句子中获得适当的值,但是对于如何使我的索引正确以将名称与最大和最小分数对齐,我有点困惑。
除了数组和索引之外,我无法使用类或其他/不同的方法。
//Create method StudentMax
private static int StudentMax(int[] Scores)
int ScoreMax = Scores[0];
for (int i = 0; i < Scores.length; i++)
if (Scores[i] > ScoreMax)
ScoreMax = Scores[i];
return ScoreMax;
//Create method StudentMin
private static int StudentMin(int[] Scores)
int ScoreMin = Scores[0];
for (int i = 0; i < Scores.length; i++)
if (Scores[i] < ScoreMin)
ScoreMin = Scores[i];
return ScoreMin;
public static void main(String[] args)
//Call Scanner
Scanner scan = new Scanner(System.in);
//User Welcome
System.out.println("Welcome to the student score sorter.");
System.out.println("\nThis program will accept a number of student names and score values, then find the highest and lowest score.");
System.out.println("\nThen will return the names and max/min scores.");
//User Prompt Enter number of students
System.out.println("\nHow many students would you like to enter: ");
int StudentCount = scan.nextInt();
//Create arrays: Scores, StudentFirst, StudentLast
int [] Scores = new int[StudentCount];
String [] StudentFirst = new String [StudentCount];
String [] StudentLast = new String [StudentCount];
for (int i = 0; i < Scores.length; i++)
System.out.println("\nStudent " + (i+1)+":");
System.out.println("\nEnter Student's name:");
StudentFirst[i] = scan.next();
StudentLast[i] = scan.next();
System.out.println("\nEnter Student's score (0-100):");
Scores[i] = scan.nextInt();
int max = StudentMax(Scores);
int min = StudentMin(Scores);
for (int i = 0; i < Scores.length; i++)
System.out.println("\n"+StudentFirst[i] + " " + StudentLast[i] +": " + Scores[i]);
for (int i = 0; i < Scores.length; i++)
if (Scores [i] == max)
System.out.println("\n"+ StudentFirst[i] +" "+ StudentLast[i] + " has the highest score => " +max+ " and " + StudentFirst[i]+" " + StudentLast[i]+ " has the lowest => " +min);
//This is the sentence format that I need to make work, but I am struggling to understand how to align the index for names and scores.
//System.out.println("\n"+StudentFirst[i] +" "+ StudentLast[i]+ " has the highest score => " +max+ " and " +StudentFirst[i] +" "+ StudentLast [i]+ " has the lowest score => " +min);
//Scan Close
scan.close();
//Close Program
【问题讨论】:
【参考方案1】:传回索引而不是值
private static int StudentMin(int[] Scores)
int ScoreMin = Scores[0];
int index = 0;
for (int i = 0; i < Scores.length; i++)
if (Scores[i] < ScoreMin)
ScoreMin = Scores[i];
index = i;
return index;
以后就可以使用了
int index = StudentMax(Scores);
System.out.println("\n"+ StudentFirst[index] +" "+ StudentLast[index] + " has the highest score => " +Scored[index]);
注意请注意Java命名约定
【讨论】:
我确实尝试过这个并且遇到了相同的问题,即 Min 索引的名称重复。我认为这适用于单个语句,但我需要在同一个句子中包含最大/最小值。 做int maxIndex = StudentMax(Scores);
然后int minIndex = StudentMin(Scores);
我觉得有点笨,但确实有效。非常感谢您的帮助和指导。在试图理解这一点的理论时,该方法是推动数组前进以便能够做到这一点,还是只是读取数组中的值并将其拉出?
如果您对这个答案感到满意,请不要忘记投票和/或接受它。
当然可以。非常感谢。这是我第一次访问该网站。以上是关于索引匹配最大/最小分数 (Java)的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 870. 优势洗牌(根据数值对索引排序)/ 856. 括号的分数(栈) / 801. 使序列递增的最小交换次数(动态规划)
LeetCode 870. 优势洗牌(根据数值对索引排序)/ 856. 括号的分数(栈) / 801. 使序列递增的最小交换次数(动态规划)