Just need some help with java Math.max and Math.min when user inputs random numbers.

## All 4 Replies

So, what is your problem? Please be specific and show examples of why these methods aren't working for you.

I am trying to just understand, if a user inputs random numbers 0 through 100 how does Math.max and Math.min work.
I know how to code if I know the numbers.
`Inline Code Example Here'
int x = 67;
int y = 23;
int y = 23;
int max = Math.max(x, y) //max is 67
int min = Math.min(x, y) //min is 23

Here is where I have gotten with my code:

``````import java.util.Scanner;
import java.lang.Math;

public class TestScoreApp
{
public static void main(String[] args)
{
// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println("To end the program enter 999.");
System.out.println();  // print a blank line

// initialize variables and create a Scanner object
int testScore = 0;
int max = Math.max(testScore, testScore);
int min = Math.min(testScore, testScore);
double scoreTotal = 0;
int scoreCount = 0;

Scanner sc = new Scanner(System.in);

// get a series of test scores from the user
while (testScore != 999)
{
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();

// accumulate score count and score total
if (testScore <= 100)
{
scoreCount += 1;
scoreTotal += testScore;
}
else if (testScore != 999)
System.out.println("Invalid entry, not counted");
}

// display the score count, score total, and average score
double averageScore = scoreTotal / scoreCount;
String message = "\n" +
"Score count:   " + scoreCount + "\n"
+ "Score total:   " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
String message2 = "\n" +
"Minimum score: " + min + "\n"
+   "Maximum score: " + max;
System.out.println(message);
System.out.println(message2);
}
}
``````

I think I figured it out? Does this look better?

``````import java.util.Scanner;
import java.lang.Math;

public class TestScoreApp
{
public static void main(String[] args)
{
// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println("To end the program enter 999.");
System.out.println();  // print a blank line

// initialize variables and create a Scanner object
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
int minScore = 0;
int maxScore = 100;

minScore = Math.min(testScore, minScore);
maxScore = Math.max(testScore, maxScore);
Scanner sc = new Scanner(System.in);

// get a series of test scores from the user
while (testScore != 999)
{
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();

// accumulate score count and score total
if (testScore <= 100)
{
scoreCount += 1;
scoreTotal += testScore;

if(minScore < testScore) {
minScore = testScore;
}
if(maxScore > testScore) {
maxScore = testScore;
}
}
else if (testScore != 999)
System.out.println("Invalid entry, not counted");
}

// display the score count, score total, and average score
double averageScore = scoreTotal / scoreCount;
String message = "\n" +
"Score count:   " + scoreCount + "\n"
+ "Score total:   " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
String message2 = "\n" +
"Minimum score: " + minScore + "\n"
+   "Maximum score: " + maxScore;
System.out.println(message);
System.out.println(message2);
}
}
``````

It would just return max - the greater of the two values and min - the smaller of them , such as

``````public int findMin(int a, int b)
if(a>b) return b;
else return a;
``````

Likewise for max

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.