# Can't figure out how to use Math.min() or return multiple names

• October 10th, 2011, 06:41 AM
Apocalypse
Can't figure out how to use Math.min() or return multiple names
Hey guys I can't seem to figure out how to get the minimum score using Math.min() I can do it if I only enter in two values but not more than than. If I enter three in it just returns the minimum value before the max in. My other problem is I can't seem to figure out how to return the names of students that have gotten two of the highest scores. For instance if a student has a high score then my program displays his/her name, but I'm not sure how to do that for lets say two or three people that got a 100 how would i be able to return all of their names. I have been trying this for hours and feel like a complete tool for not being able to figure this out. I'm pretty sure its a somewhat simple solution but i just can't seem to wrap my head around it. Any help would be appreciated. Thanks Guys! Here's my code:

import java.util.Scanner;

public class Assignment2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int numberOfExams = 0;

System.out.print("Please Enter The Number Of Exams You Wish To Enter: ");
numberOfExams = input.nextInt();

while(numberOfExams != 0)
{
String asteriskA = "";
String asteriskB = "";
String asteriskC = "";
String asteriskD = "";
String asteriskF = "";
String name = " ";
String newName = " ";
int gotAnA = 0;
int gotAB = 0;
int gotAC = 0;
int gotAnF = 0;
double score = 0;
double total = 0;
double maximum = 0;
double minimum = 0;
double average = 0;

for(int i = 1; i <= numberOfExams; i++)
{
name = input.next();
score = input.nextDouble();
total += score;
average = total/numberOfExams;
//minimum = score;
//minimum = Math.min(score, score);
maximum = Math.max(maximum, score);

if(score > 89)
{
gotAnA++;
asteriskA += "*";
}
else if(score > 79)
{
gotAB++;
asteriskB += "*";
}
else if(score > 69)
{
gotAC++;
asteriskC += "*";
}
else if(score > 59)
{
asteriskD += "*";
}
else
{
gotAnF++;
asteriskF += "*";
}
}

System.out.println("\nThe Total For Your " + numberOfExams + " Exam Scores Is: " + total);
System.out.println("The Average Of Your Exam Scores Is: " + average);
System.out.println("The Minimum Exam Score Entered In This Set Is: " + minimum);
System.out.println("The Maximum Exam Score Entered In This Set Is: " + maximum + " by " + newName);
System.out.println("\n" + gotAnA + " Got An A.");
System.out.println(gotAB + " Got A B.");
System.out.println(gotAC + " Got A C.");
System.out.println(gotAD + " Got A D.");
System.out.println(gotAnF + " Got An F.");
System.out.println("\nA: " + asteriskA);
System.out.println("B: " + asteriskB);
System.out.println("C: " + asteriskC);
System.out.println("D: " + asteriskD);
System.out.println("F: " + asteriskF);

System.out.print("\nPlease Enter The Number Of Exams You Wish To Enter: ");
numberOfExams = input.nextInt();

}

if(numberOfExams == 0)
{
System.out.print("\nThank You For Using This Program! Have A Nice Day!");
}

}
}
• October 10th, 2011, 07:17 AM
KevinWorkman
Re: Can't figure out how to use Math.min() or return multiple names
You seemed to be comparing score with itself in the Math.min() function. Is that really what you want to do?

And for your other question, how would you do it by hand, without a computer?
• October 10th, 2011, 10:21 AM
Apocalypse
Re: Can't figure out how to use Math.min() or return multiple names
For the minimum I've actually tried every combination, the one you see commented out is the one that I was currently at. It won't compare anything right because minimum is set to zero in which case it will always comeback as the minimum. As for the names I think Im getting somewhere with that one. By the way, thanks for the reply and help.
• October 10th, 2011, 11:25 AM
KevinWorkman
Re: Can't figure out how to use Math.min() or return multiple names