Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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 gotAD = 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++)
    {
    System.out.print("Please Enter Your Students Name And Grade Seperated By A Space: ");
    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)
    {
    gotAD++;
    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!");
    }

    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    Well then why set it to zero to start with?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Listing file names in a JList
    By KILL3RTACO in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 8th, 2011, 12:52 PM
  2. Replies: 5
    Last Post: August 28th, 2011, 04:48 PM
  3. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  4. array to store multiple names and data
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 6th, 2011, 05:03 AM
  5. regular expressions, characters unallowed in file names
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:17 PM