Maximum and minimum method java











up vote
-4
down vote

favorite












This program is supposed to find the maximum, minimum, and average of grades. User inputs int inputGrade and the program displays letter it is. It's supposed to do this how however many students are needed. I'm having trouble writing the method where it finds the max and min. (yes I've talked to my teacher if anyone's wondering...) I pasted the methods below (they don't work). Just like IN GENERAL, does anyone know how to find the maximum and minimum of a set of entered numbers? (not using arrays, lists, or any unusual imports other than scanner) ** note I've updated this a lot...



import java.util.Scanner;
public class GetLetterGrade
{
static int inputGrade; // input grade

public static void main(String args)
{
Scanner reader = new Scanner(System.in);


int classAverage;
int classMin; // class's minimum grade
int classMax; // class's maximum grade

while (inputGrade != -1) // while user is entering grades
{

System.out.println("Welcome to the grade calculator. nPlease enter a
numeric grade. After the last student in the class, enter a grade of
-1.");
inputGrade = reader.nextInt();
letterGrade(inputGrade); // calls letter grade method
findMaxAndMin();
result();
}

}
// find letter grade
public static String letterGrade(int numGrade)
{

String gradeMessage = "";

{

if (numGrade >= 96 && numGrade <= 100) // if numeric grade is 96-100 then
it's A+
{
gradeMessage = "That's an A+.";
result();
// DOES THIS FOR GRADES A+ TO F, NOT SHOWN, too much to paste!
}

}
}
return gradeMessage;
}

public static int findCharGrade(int numGrade)
{
char letter;
if (numGrade >= 90 && numGrade <= 100) // A
{
letter = 'A';

}
else if (numGrade >= 80 && numGrade < 90) // B
{
letter = 'B';
}
else if (numGrade >= 70 && numGrade < 80) // C
{
letter = 'C';

}
else if (numGrade >= 60 && numGrade < 70) // D
{
letter = 'D';
}
else if (numGrade < 60) // F
{
letter = 'F';
}

}




// finds maximum and minimum grades
public static int findMaxAndMin(int inputGrade)
{

int max = Math.max(inputGrade, max);
int min = Math.min(inputGrade, min);

if (inputGrade < max)
{
inputGrade = max;
findCharGrade(inputGrade);
}
else if (inputGrade > min)
{
inputGrade = min;
findCharGrade(inputGrade);
}

}
public static void calcAverage(int sumOfGrades, int numOfStudents)
{
// something goes here

}



// finds results
public static void result()
{
int min = findMaxAndMin(inputGrade);
int max = findMaxAndMin(inputGrade);
System.out.println("Please enter a numeric grade");
int inputGrade = reader.nextInt();
letterGrade(inputGrade);


if (inputGrade == -1)
{
System.out.println("You entered " + numOfStudents + " students. Class
Average: " + average + " Class Minimum: " + min + " Class maximum: " + max
+ " nThanks for using the class grade calculator!");
}

}









share|improve this question
























  • What does "they don't work" mean? What's not working?
    – natn2323
    Nov 15 at 0:17










  • you should initialize the max and min the other way so that the function would work i.e. max = int.min_val, min = int.max_val. and invert the if clause as well
    – Liad Saubron
    Nov 15 at 0:19








  • 1




    Can you post the entire class? We have no idea what the inputGrade is.
    – PhaseRush
    Nov 15 at 0:20






  • 1




    Your code would be a lot easier to read if the indentation made sense.
    – khelwood
    Nov 15 at 0:26















up vote
-4
down vote

favorite












This program is supposed to find the maximum, minimum, and average of grades. User inputs int inputGrade and the program displays letter it is. It's supposed to do this how however many students are needed. I'm having trouble writing the method where it finds the max and min. (yes I've talked to my teacher if anyone's wondering...) I pasted the methods below (they don't work). Just like IN GENERAL, does anyone know how to find the maximum and minimum of a set of entered numbers? (not using arrays, lists, or any unusual imports other than scanner) ** note I've updated this a lot...



import java.util.Scanner;
public class GetLetterGrade
{
static int inputGrade; // input grade

public static void main(String args)
{
Scanner reader = new Scanner(System.in);


int classAverage;
int classMin; // class's minimum grade
int classMax; // class's maximum grade

while (inputGrade != -1) // while user is entering grades
{

System.out.println("Welcome to the grade calculator. nPlease enter a
numeric grade. After the last student in the class, enter a grade of
-1.");
inputGrade = reader.nextInt();
letterGrade(inputGrade); // calls letter grade method
findMaxAndMin();
result();
}

}
// find letter grade
public static String letterGrade(int numGrade)
{

String gradeMessage = "";

{

if (numGrade >= 96 && numGrade <= 100) // if numeric grade is 96-100 then
it's A+
{
gradeMessage = "That's an A+.";
result();
// DOES THIS FOR GRADES A+ TO F, NOT SHOWN, too much to paste!
}

}
}
return gradeMessage;
}

public static int findCharGrade(int numGrade)
{
char letter;
if (numGrade >= 90 && numGrade <= 100) // A
{
letter = 'A';

}
else if (numGrade >= 80 && numGrade < 90) // B
{
letter = 'B';
}
else if (numGrade >= 70 && numGrade < 80) // C
{
letter = 'C';

}
else if (numGrade >= 60 && numGrade < 70) // D
{
letter = 'D';
}
else if (numGrade < 60) // F
{
letter = 'F';
}

}




// finds maximum and minimum grades
public static int findMaxAndMin(int inputGrade)
{

int max = Math.max(inputGrade, max);
int min = Math.min(inputGrade, min);

if (inputGrade < max)
{
inputGrade = max;
findCharGrade(inputGrade);
}
else if (inputGrade > min)
{
inputGrade = min;
findCharGrade(inputGrade);
}

}
public static void calcAverage(int sumOfGrades, int numOfStudents)
{
// something goes here

}



// finds results
public static void result()
{
int min = findMaxAndMin(inputGrade);
int max = findMaxAndMin(inputGrade);
System.out.println("Please enter a numeric grade");
int inputGrade = reader.nextInt();
letterGrade(inputGrade);


if (inputGrade == -1)
{
System.out.println("You entered " + numOfStudents + " students. Class
Average: " + average + " Class Minimum: " + min + " Class maximum: " + max
+ " nThanks for using the class grade calculator!");
}

}









share|improve this question
























  • What does "they don't work" mean? What's not working?
    – natn2323
    Nov 15 at 0:17










  • you should initialize the max and min the other way so that the function would work i.e. max = int.min_val, min = int.max_val. and invert the if clause as well
    – Liad Saubron
    Nov 15 at 0:19








  • 1




    Can you post the entire class? We have no idea what the inputGrade is.
    – PhaseRush
    Nov 15 at 0:20






  • 1




    Your code would be a lot easier to read if the indentation made sense.
    – khelwood
    Nov 15 at 0:26













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











This program is supposed to find the maximum, minimum, and average of grades. User inputs int inputGrade and the program displays letter it is. It's supposed to do this how however many students are needed. I'm having trouble writing the method where it finds the max and min. (yes I've talked to my teacher if anyone's wondering...) I pasted the methods below (they don't work). Just like IN GENERAL, does anyone know how to find the maximum and minimum of a set of entered numbers? (not using arrays, lists, or any unusual imports other than scanner) ** note I've updated this a lot...



import java.util.Scanner;
public class GetLetterGrade
{
static int inputGrade; // input grade

public static void main(String args)
{
Scanner reader = new Scanner(System.in);


int classAverage;
int classMin; // class's minimum grade
int classMax; // class's maximum grade

while (inputGrade != -1) // while user is entering grades
{

System.out.println("Welcome to the grade calculator. nPlease enter a
numeric grade. After the last student in the class, enter a grade of
-1.");
inputGrade = reader.nextInt();
letterGrade(inputGrade); // calls letter grade method
findMaxAndMin();
result();
}

}
// find letter grade
public static String letterGrade(int numGrade)
{

String gradeMessage = "";

{

if (numGrade >= 96 && numGrade <= 100) // if numeric grade is 96-100 then
it's A+
{
gradeMessage = "That's an A+.";
result();
// DOES THIS FOR GRADES A+ TO F, NOT SHOWN, too much to paste!
}

}
}
return gradeMessage;
}

public static int findCharGrade(int numGrade)
{
char letter;
if (numGrade >= 90 && numGrade <= 100) // A
{
letter = 'A';

}
else if (numGrade >= 80 && numGrade < 90) // B
{
letter = 'B';
}
else if (numGrade >= 70 && numGrade < 80) // C
{
letter = 'C';

}
else if (numGrade >= 60 && numGrade < 70) // D
{
letter = 'D';
}
else if (numGrade < 60) // F
{
letter = 'F';
}

}




// finds maximum and minimum grades
public static int findMaxAndMin(int inputGrade)
{

int max = Math.max(inputGrade, max);
int min = Math.min(inputGrade, min);

if (inputGrade < max)
{
inputGrade = max;
findCharGrade(inputGrade);
}
else if (inputGrade > min)
{
inputGrade = min;
findCharGrade(inputGrade);
}

}
public static void calcAverage(int sumOfGrades, int numOfStudents)
{
// something goes here

}



// finds results
public static void result()
{
int min = findMaxAndMin(inputGrade);
int max = findMaxAndMin(inputGrade);
System.out.println("Please enter a numeric grade");
int inputGrade = reader.nextInt();
letterGrade(inputGrade);


if (inputGrade == -1)
{
System.out.println("You entered " + numOfStudents + " students. Class
Average: " + average + " Class Minimum: " + min + " Class maximum: " + max
+ " nThanks for using the class grade calculator!");
}

}









share|improve this question















This program is supposed to find the maximum, minimum, and average of grades. User inputs int inputGrade and the program displays letter it is. It's supposed to do this how however many students are needed. I'm having trouble writing the method where it finds the max and min. (yes I've talked to my teacher if anyone's wondering...) I pasted the methods below (they don't work). Just like IN GENERAL, does anyone know how to find the maximum and minimum of a set of entered numbers? (not using arrays, lists, or any unusual imports other than scanner) ** note I've updated this a lot...



import java.util.Scanner;
public class GetLetterGrade
{
static int inputGrade; // input grade

public static void main(String args)
{
Scanner reader = new Scanner(System.in);


int classAverage;
int classMin; // class's minimum grade
int classMax; // class's maximum grade

while (inputGrade != -1) // while user is entering grades
{

System.out.println("Welcome to the grade calculator. nPlease enter a
numeric grade. After the last student in the class, enter a grade of
-1.");
inputGrade = reader.nextInt();
letterGrade(inputGrade); // calls letter grade method
findMaxAndMin();
result();
}

}
// find letter grade
public static String letterGrade(int numGrade)
{

String gradeMessage = "";

{

if (numGrade >= 96 && numGrade <= 100) // if numeric grade is 96-100 then
it's A+
{
gradeMessage = "That's an A+.";
result();
// DOES THIS FOR GRADES A+ TO F, NOT SHOWN, too much to paste!
}

}
}
return gradeMessage;
}

public static int findCharGrade(int numGrade)
{
char letter;
if (numGrade >= 90 && numGrade <= 100) // A
{
letter = 'A';

}
else if (numGrade >= 80 && numGrade < 90) // B
{
letter = 'B';
}
else if (numGrade >= 70 && numGrade < 80) // C
{
letter = 'C';

}
else if (numGrade >= 60 && numGrade < 70) // D
{
letter = 'D';
}
else if (numGrade < 60) // F
{
letter = 'F';
}

}




// finds maximum and minimum grades
public static int findMaxAndMin(int inputGrade)
{

int max = Math.max(inputGrade, max);
int min = Math.min(inputGrade, min);

if (inputGrade < max)
{
inputGrade = max;
findCharGrade(inputGrade);
}
else if (inputGrade > min)
{
inputGrade = min;
findCharGrade(inputGrade);
}

}
public static void calcAverage(int sumOfGrades, int numOfStudents)
{
// something goes here

}



// finds results
public static void result()
{
int min = findMaxAndMin(inputGrade);
int max = findMaxAndMin(inputGrade);
System.out.println("Please enter a numeric grade");
int inputGrade = reader.nextInt();
letterGrade(inputGrade);


if (inputGrade == -1)
{
System.out.println("You entered " + numOfStudents + " students. Class
Average: " + average + " Class Minimum: " + min + " Class maximum: " + max
+ " nThanks for using the class grade calculator!");
}

}






java max min






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 3:56

























asked Nov 15 at 0:14









Janie

13




13












  • What does "they don't work" mean? What's not working?
    – natn2323
    Nov 15 at 0:17










  • you should initialize the max and min the other way so that the function would work i.e. max = int.min_val, min = int.max_val. and invert the if clause as well
    – Liad Saubron
    Nov 15 at 0:19








  • 1




    Can you post the entire class? We have no idea what the inputGrade is.
    – PhaseRush
    Nov 15 at 0:20






  • 1




    Your code would be a lot easier to read if the indentation made sense.
    – khelwood
    Nov 15 at 0:26


















  • What does "they don't work" mean? What's not working?
    – natn2323
    Nov 15 at 0:17










  • you should initialize the max and min the other way so that the function would work i.e. max = int.min_val, min = int.max_val. and invert the if clause as well
    – Liad Saubron
    Nov 15 at 0:19








  • 1




    Can you post the entire class? We have no idea what the inputGrade is.
    – PhaseRush
    Nov 15 at 0:20






  • 1




    Your code would be a lot easier to read if the indentation made sense.
    – khelwood
    Nov 15 at 0:26
















What does "they don't work" mean? What's not working?
– natn2323
Nov 15 at 0:17




What does "they don't work" mean? What's not working?
– natn2323
Nov 15 at 0:17












you should initialize the max and min the other way so that the function would work i.e. max = int.min_val, min = int.max_val. and invert the if clause as well
– Liad Saubron
Nov 15 at 0:19






you should initialize the max and min the other way so that the function would work i.e. max = int.min_val, min = int.max_val. and invert the if clause as well
– Liad Saubron
Nov 15 at 0:19






1




1




Can you post the entire class? We have no idea what the inputGrade is.
– PhaseRush
Nov 15 at 0:20




Can you post the entire class? We have no idea what the inputGrade is.
– PhaseRush
Nov 15 at 0:20




1




1




Your code would be a lot easier to read if the indentation made sense.
– khelwood
Nov 15 at 0:26




Your code would be a lot easier to read if the indentation made sense.
– khelwood
Nov 15 at 0:26












3 Answers
3






active

oldest

votes

















up vote
1
down vote













here is a more simplistic way of doing it not using Lists or arrays



    double sum = 0;  // use double so that you do not do integer arithmetic
int count = 0;
int min = Integer.MAX_VALUE; // set to very high value
int max = Integer.MIN_VALUE; // set to bery low value

Scanner scan1 = new Scanner(System.in);
System.out.println("enter numbers (-1 to quit");

while (scan1.hasNextInt()) {
int i = scan1.nextInt(); // get the number (assuming only int value)
if (i == -1) break;

min = Math.min(i, min);
max = Math.max(i, max);
sum += i;
count++;
}

if (count > 0) {
System.out.println("min " + min);
System.out.println("max " + max);
System.out.println("avg " + sum / count);
}


disclaimer



This code will not handle wrong type of input e.g. Strings



edit



If you want the average to be calculated in a separate method you can have a method like



double calcAvg (double sum, int count) {
return sum / count;
}


this can then be called as



    if (count > 0) {
System.out.println("min " + min);
System.out.println("max " + max);
System.out.println("avg " + calcAvg (sum, count));
}





share|improve this answer























  • wow I didn't even know that Math.min and Math.max existed. super helpful thanks
    – Janie
    Nov 15 at 1:12










  • @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
    – zlakad
    Nov 15 at 1:35










  • ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
    – Janie
    Nov 15 at 1:43












  • Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
    – Scary Wombat
    Nov 15 at 1:49








  • 1




    @zlakad Cheers, too tired - didn't think. My bad....
    – Scary Wombat
    Nov 15 at 2:17


















up vote
0
down vote













You can (and should) divide your problem into the smaller methods.
I'll drop the code, and you read and study it.
I admit I haven't pay to much attention of this simple quest, but still...
Here you are:



import java.util.List;

public class Answer {

public static void main(String args) {

//test with some grades (integers)
Answer answer = new Answer();
List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
System.out.println("max = " + answer.findMaxGrade(someGrades));
System.out.println("min = " + answer.findMinGrade(someGrades));
System.out.println("avg = " + answer.findAverageGrade(someGrades));

}

private int findMaxGrade(List<Integer> grades) {
int max = Integer.MIN_VALUE;
for (int grade : grades) {
if (grade > max) max = grade;
}
return max;
}

private int findMinGrade(List<Integer> grades) {
int min = Integer.MAX_VALUE;
for (int grade : grades) {
if (grade < min) min = grade;
}
return min;
}

private double findAverageGrade(List<Integer> grades) {
double average = 0;
for (int grade : grades) {
average += grade;
}
return average / grades.size();
}
}





share|improve this answer





















  • Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
    – Janie
    Nov 15 at 0:54










  • @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
    – zlakad
    Nov 15 at 0:56










  • is there a way to do this without lists or arrays?
    – Janie
    Nov 15 at 0:56












  • @Janie, of course it is! Just change the methods parameter to int grades.
    – zlakad
    Nov 15 at 0:58










  • I haven't ever used the int before. Is that an array? @zlakad
    – Janie
    Nov 15 at 0:58


















up vote
0
down vote













package example;



import java.util.Scanner;



class Example {



public static void main(String args) {
Scanner r = new Scanner(System.in);
int m = 1, total = 0, max = 0, min = 100;
double avg = 0;
while (m <= 5) {
System.out.print("Input marks " + m + " = ");
int inp = r.nextInt();
total += inp;
m++;
min=min<inp?min:inp;
max=max<inp?inp:max;


}
avg = (double)(total) / 5;
System.out.println("Total : " + total);
System.out.println("Max : " + max);
System.out.println("Min : " + min);
System.out.println("Average : " + avg);

}


}






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310675%2fmaximum-and-minimum-method-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    here is a more simplistic way of doing it not using Lists or arrays



        double sum = 0;  // use double so that you do not do integer arithmetic
    int count = 0;
    int min = Integer.MAX_VALUE; // set to very high value
    int max = Integer.MIN_VALUE; // set to bery low value

    Scanner scan1 = new Scanner(System.in);
    System.out.println("enter numbers (-1 to quit");

    while (scan1.hasNextInt()) {
    int i = scan1.nextInt(); // get the number (assuming only int value)
    if (i == -1) break;

    min = Math.min(i, min);
    max = Math.max(i, max);
    sum += i;
    count++;
    }

    if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + sum / count);
    }


    disclaimer



    This code will not handle wrong type of input e.g. Strings



    edit



    If you want the average to be calculated in a separate method you can have a method like



    double calcAvg (double sum, int count) {
    return sum / count;
    }


    this can then be called as



        if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + calcAvg (sum, count));
    }





    share|improve this answer























    • wow I didn't even know that Math.min and Math.max existed. super helpful thanks
      – Janie
      Nov 15 at 1:12










    • @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
      – zlakad
      Nov 15 at 1:35










    • ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
      – Janie
      Nov 15 at 1:43












    • Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
      – Scary Wombat
      Nov 15 at 1:49








    • 1




      @zlakad Cheers, too tired - didn't think. My bad....
      – Scary Wombat
      Nov 15 at 2:17















    up vote
    1
    down vote













    here is a more simplistic way of doing it not using Lists or arrays



        double sum = 0;  // use double so that you do not do integer arithmetic
    int count = 0;
    int min = Integer.MAX_VALUE; // set to very high value
    int max = Integer.MIN_VALUE; // set to bery low value

    Scanner scan1 = new Scanner(System.in);
    System.out.println("enter numbers (-1 to quit");

    while (scan1.hasNextInt()) {
    int i = scan1.nextInt(); // get the number (assuming only int value)
    if (i == -1) break;

    min = Math.min(i, min);
    max = Math.max(i, max);
    sum += i;
    count++;
    }

    if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + sum / count);
    }


    disclaimer



    This code will not handle wrong type of input e.g. Strings



    edit



    If you want the average to be calculated in a separate method you can have a method like



    double calcAvg (double sum, int count) {
    return sum / count;
    }


    this can then be called as



        if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + calcAvg (sum, count));
    }





    share|improve this answer























    • wow I didn't even know that Math.min and Math.max existed. super helpful thanks
      – Janie
      Nov 15 at 1:12










    • @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
      – zlakad
      Nov 15 at 1:35










    • ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
      – Janie
      Nov 15 at 1:43












    • Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
      – Scary Wombat
      Nov 15 at 1:49








    • 1




      @zlakad Cheers, too tired - didn't think. My bad....
      – Scary Wombat
      Nov 15 at 2:17













    up vote
    1
    down vote










    up vote
    1
    down vote









    here is a more simplistic way of doing it not using Lists or arrays



        double sum = 0;  // use double so that you do not do integer arithmetic
    int count = 0;
    int min = Integer.MAX_VALUE; // set to very high value
    int max = Integer.MIN_VALUE; // set to bery low value

    Scanner scan1 = new Scanner(System.in);
    System.out.println("enter numbers (-1 to quit");

    while (scan1.hasNextInt()) {
    int i = scan1.nextInt(); // get the number (assuming only int value)
    if (i == -1) break;

    min = Math.min(i, min);
    max = Math.max(i, max);
    sum += i;
    count++;
    }

    if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + sum / count);
    }


    disclaimer



    This code will not handle wrong type of input e.g. Strings



    edit



    If you want the average to be calculated in a separate method you can have a method like



    double calcAvg (double sum, int count) {
    return sum / count;
    }


    this can then be called as



        if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + calcAvg (sum, count));
    }





    share|improve this answer














    here is a more simplistic way of doing it not using Lists or arrays



        double sum = 0;  // use double so that you do not do integer arithmetic
    int count = 0;
    int min = Integer.MAX_VALUE; // set to very high value
    int max = Integer.MIN_VALUE; // set to bery low value

    Scanner scan1 = new Scanner(System.in);
    System.out.println("enter numbers (-1 to quit");

    while (scan1.hasNextInt()) {
    int i = scan1.nextInt(); // get the number (assuming only int value)
    if (i == -1) break;

    min = Math.min(i, min);
    max = Math.max(i, max);
    sum += i;
    count++;
    }

    if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + sum / count);
    }


    disclaimer



    This code will not handle wrong type of input e.g. Strings



    edit



    If you want the average to be calculated in a separate method you can have a method like



    double calcAvg (double sum, int count) {
    return sum / count;
    }


    this can then be called as



        if (count > 0) {
    System.out.println("min " + min);
    System.out.println("max " + max);
    System.out.println("avg " + calcAvg (sum, count));
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 at 2:17

























    answered Nov 15 at 1:08









    Scary Wombat

    34.8k32252




    34.8k32252












    • wow I didn't even know that Math.min and Math.max existed. super helpful thanks
      – Janie
      Nov 15 at 1:12










    • @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
      – zlakad
      Nov 15 at 1:35










    • ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
      – Janie
      Nov 15 at 1:43












    • Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
      – Scary Wombat
      Nov 15 at 1:49








    • 1




      @zlakad Cheers, too tired - didn't think. My bad....
      – Scary Wombat
      Nov 15 at 2:17


















    • wow I didn't even know that Math.min and Math.max existed. super helpful thanks
      – Janie
      Nov 15 at 1:12










    • @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
      – zlakad
      Nov 15 at 1:35










    • ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
      – Janie
      Nov 15 at 1:43












    • Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
      – Scary Wombat
      Nov 15 at 1:49








    • 1




      @zlakad Cheers, too tired - didn't think. My bad....
      – Scary Wombat
      Nov 15 at 2:17
















    wow I didn't even know that Math.min and Math.max existed. super helpful thanks
    – Janie
    Nov 15 at 1:12




    wow I didn't even know that Math.min and Math.max existed. super helpful thanks
    – Janie
    Nov 15 at 1:12












    @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
    – zlakad
    Nov 15 at 1:35




    @Janie, if you are scared to use the Math.min you can use if (i < min) min = i; (Similar approach to max). And, yes, accept this answer!
    – zlakad
    Nov 15 at 1:35












    ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
    – Janie
    Nov 15 at 1:43






    ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad
    – Janie
    Nov 15 at 1:43














    Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
    – Scary Wombat
    Nov 15 at 1:49






    Do you want a running average without using a List or Array? Just put the code for System.out.println("avg " + sum / count); into a method
    – Scary Wombat
    Nov 15 at 1:49






    1




    1




    @zlakad Cheers, too tired - didn't think. My bad....
    – Scary Wombat
    Nov 15 at 2:17




    @zlakad Cheers, too tired - didn't think. My bad....
    – Scary Wombat
    Nov 15 at 2:17












    up vote
    0
    down vote













    You can (and should) divide your problem into the smaller methods.
    I'll drop the code, and you read and study it.
    I admit I haven't pay to much attention of this simple quest, but still...
    Here you are:



    import java.util.List;

    public class Answer {

    public static void main(String args) {

    //test with some grades (integers)
    Answer answer = new Answer();
    List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
    System.out.println("max = " + answer.findMaxGrade(someGrades));
    System.out.println("min = " + answer.findMinGrade(someGrades));
    System.out.println("avg = " + answer.findAverageGrade(someGrades));

    }

    private int findMaxGrade(List<Integer> grades) {
    int max = Integer.MIN_VALUE;
    for (int grade : grades) {
    if (grade > max) max = grade;
    }
    return max;
    }

    private int findMinGrade(List<Integer> grades) {
    int min = Integer.MAX_VALUE;
    for (int grade : grades) {
    if (grade < min) min = grade;
    }
    return min;
    }

    private double findAverageGrade(List<Integer> grades) {
    double average = 0;
    for (int grade : grades) {
    average += grade;
    }
    return average / grades.size();
    }
    }





    share|improve this answer





















    • Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
      – Janie
      Nov 15 at 0:54










    • @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
      – zlakad
      Nov 15 at 0:56










    • is there a way to do this without lists or arrays?
      – Janie
      Nov 15 at 0:56












    • @Janie, of course it is! Just change the methods parameter to int grades.
      – zlakad
      Nov 15 at 0:58










    • I haven't ever used the int before. Is that an array? @zlakad
      – Janie
      Nov 15 at 0:58















    up vote
    0
    down vote













    You can (and should) divide your problem into the smaller methods.
    I'll drop the code, and you read and study it.
    I admit I haven't pay to much attention of this simple quest, but still...
    Here you are:



    import java.util.List;

    public class Answer {

    public static void main(String args) {

    //test with some grades (integers)
    Answer answer = new Answer();
    List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
    System.out.println("max = " + answer.findMaxGrade(someGrades));
    System.out.println("min = " + answer.findMinGrade(someGrades));
    System.out.println("avg = " + answer.findAverageGrade(someGrades));

    }

    private int findMaxGrade(List<Integer> grades) {
    int max = Integer.MIN_VALUE;
    for (int grade : grades) {
    if (grade > max) max = grade;
    }
    return max;
    }

    private int findMinGrade(List<Integer> grades) {
    int min = Integer.MAX_VALUE;
    for (int grade : grades) {
    if (grade < min) min = grade;
    }
    return min;
    }

    private double findAverageGrade(List<Integer> grades) {
    double average = 0;
    for (int grade : grades) {
    average += grade;
    }
    return average / grades.size();
    }
    }





    share|improve this answer





















    • Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
      – Janie
      Nov 15 at 0:54










    • @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
      – zlakad
      Nov 15 at 0:56










    • is there a way to do this without lists or arrays?
      – Janie
      Nov 15 at 0:56












    • @Janie, of course it is! Just change the methods parameter to int grades.
      – zlakad
      Nov 15 at 0:58










    • I haven't ever used the int before. Is that an array? @zlakad
      – Janie
      Nov 15 at 0:58













    up vote
    0
    down vote










    up vote
    0
    down vote









    You can (and should) divide your problem into the smaller methods.
    I'll drop the code, and you read and study it.
    I admit I haven't pay to much attention of this simple quest, but still...
    Here you are:



    import java.util.List;

    public class Answer {

    public static void main(String args) {

    //test with some grades (integers)
    Answer answer = new Answer();
    List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
    System.out.println("max = " + answer.findMaxGrade(someGrades));
    System.out.println("min = " + answer.findMinGrade(someGrades));
    System.out.println("avg = " + answer.findAverageGrade(someGrades));

    }

    private int findMaxGrade(List<Integer> grades) {
    int max = Integer.MIN_VALUE;
    for (int grade : grades) {
    if (grade > max) max = grade;
    }
    return max;
    }

    private int findMinGrade(List<Integer> grades) {
    int min = Integer.MAX_VALUE;
    for (int grade : grades) {
    if (grade < min) min = grade;
    }
    return min;
    }

    private double findAverageGrade(List<Integer> grades) {
    double average = 0;
    for (int grade : grades) {
    average += grade;
    }
    return average / grades.size();
    }
    }





    share|improve this answer












    You can (and should) divide your problem into the smaller methods.
    I'll drop the code, and you read and study it.
    I admit I haven't pay to much attention of this simple quest, but still...
    Here you are:



    import java.util.List;

    public class Answer {

    public static void main(String args) {

    //test with some grades (integers)
    Answer answer = new Answer();
    List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
    System.out.println("max = " + answer.findMaxGrade(someGrades));
    System.out.println("min = " + answer.findMinGrade(someGrades));
    System.out.println("avg = " + answer.findAverageGrade(someGrades));

    }

    private int findMaxGrade(List<Integer> grades) {
    int max = Integer.MIN_VALUE;
    for (int grade : grades) {
    if (grade > max) max = grade;
    }
    return max;
    }

    private int findMinGrade(List<Integer> grades) {
    int min = Integer.MAX_VALUE;
    for (int grade : grades) {
    if (grade < min) min = grade;
    }
    return min;
    }

    private double findAverageGrade(List<Integer> grades) {
    double average = 0;
    for (int grade : grades) {
    average += grade;
    }
    return average / grades.size();
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 at 0:47









    zlakad

    1,0681715




    1,0681715












    • Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
      – Janie
      Nov 15 at 0:54










    • @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
      – zlakad
      Nov 15 at 0:56










    • is there a way to do this without lists or arrays?
      – Janie
      Nov 15 at 0:56












    • @Janie, of course it is! Just change the methods parameter to int grades.
      – zlakad
      Nov 15 at 0:58










    • I haven't ever used the int before. Is that an array? @zlakad
      – Janie
      Nov 15 at 0:58


















    • Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
      – Janie
      Nov 15 at 0:54










    • @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
      – zlakad
      Nov 15 at 0:56










    • is there a way to do this without lists or arrays?
      – Janie
      Nov 15 at 0:56












    • @Janie, of course it is! Just change the methods parameter to int grades.
      – zlakad
      Nov 15 at 0:58










    • I haven't ever used the int before. Is that an array? @zlakad
      – Janie
      Nov 15 at 0:58
















    Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
    – Janie
    Nov 15 at 0:54




    Thanks for your response, but I haven't covered lists in my APCS class. I'll try to use this though!
    – Janie
    Nov 15 at 0:54












    @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
    – zlakad
    Nov 15 at 0:56




    @Janie, feel free to ask me anything else (for example, you can put your grades into the int array instead of list)
    – zlakad
    Nov 15 at 0:56












    is there a way to do this without lists or arrays?
    – Janie
    Nov 15 at 0:56






    is there a way to do this without lists or arrays?
    – Janie
    Nov 15 at 0:56














    @Janie, of course it is! Just change the methods parameter to int grades.
    – zlakad
    Nov 15 at 0:58




    @Janie, of course it is! Just change the methods parameter to int grades.
    – zlakad
    Nov 15 at 0:58












    I haven't ever used the int before. Is that an array? @zlakad
    – Janie
    Nov 15 at 0:58




    I haven't ever used the int before. Is that an array? @zlakad
    – Janie
    Nov 15 at 0:58










    up vote
    0
    down vote













    package example;



    import java.util.Scanner;



    class Example {



    public static void main(String args) {
    Scanner r = new Scanner(System.in);
    int m = 1, total = 0, max = 0, min = 100;
    double avg = 0;
    while (m <= 5) {
    System.out.print("Input marks " + m + " = ");
    int inp = r.nextInt();
    total += inp;
    m++;
    min=min<inp?min:inp;
    max=max<inp?inp:max;


    }
    avg = (double)(total) / 5;
    System.out.println("Total : " + total);
    System.out.println("Max : " + max);
    System.out.println("Min : " + min);
    System.out.println("Average : " + avg);

    }


    }






    share|improve this answer

























      up vote
      0
      down vote













      package example;



      import java.util.Scanner;



      class Example {



      public static void main(String args) {
      Scanner r = new Scanner(System.in);
      int m = 1, total = 0, max = 0, min = 100;
      double avg = 0;
      while (m <= 5) {
      System.out.print("Input marks " + m + " = ");
      int inp = r.nextInt();
      total += inp;
      m++;
      min=min<inp?min:inp;
      max=max<inp?inp:max;


      }
      avg = (double)(total) / 5;
      System.out.println("Total : " + total);
      System.out.println("Max : " + max);
      System.out.println("Min : " + min);
      System.out.println("Average : " + avg);

      }


      }






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        package example;



        import java.util.Scanner;



        class Example {



        public static void main(String args) {
        Scanner r = new Scanner(System.in);
        int m = 1, total = 0, max = 0, min = 100;
        double avg = 0;
        while (m <= 5) {
        System.out.print("Input marks " + m + " = ");
        int inp = r.nextInt();
        total += inp;
        m++;
        min=min<inp?min:inp;
        max=max<inp?inp:max;


        }
        avg = (double)(total) / 5;
        System.out.println("Total : " + total);
        System.out.println("Max : " + max);
        System.out.println("Min : " + min);
        System.out.println("Average : " + avg);

        }


        }






        share|improve this answer












        package example;



        import java.util.Scanner;



        class Example {



        public static void main(String args) {
        Scanner r = new Scanner(System.in);
        int m = 1, total = 0, max = 0, min = 100;
        double avg = 0;
        while (m <= 5) {
        System.out.print("Input marks " + m + " = ");
        int inp = r.nextInt();
        total += inp;
        m++;
        min=min<inp?min:inp;
        max=max<inp?inp:max;


        }
        avg = (double)(total) / 5;
        System.out.println("Total : " + total);
        System.out.println("Max : " + max);
        System.out.println("Min : " + min);
        System.out.println("Average : " + avg);

        }


        }







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 2:42









        Roshan Tharanga

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310675%2fmaximum-and-minimum-method-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?