0

I am creating a program where I must get the scores from several users and then print the highest and lowest score. I have attempted to do this by creating the loop and asking for the score and name then creating new variables for the high and lows attempted both in and out of the loop but it has not worked any help would be greatly appreciated!

/* Name: Armaan and Kenny * Date: * Course: * Description: */

import hsa.Console;
import java.awt.*;

public class testPilo
{
  static Console c;
  public static void main(String[] args)
  { 
    c = new Console();
    int i = 0;
    int a = 0;
    int y = 0;
    String x = "";
    String bigBoy = "";
    String lilBoy = "";

    int highest = 0;
    int lowest = 0;
    c.println("How man players do you want?");
    a = c.readInt();

    while(true){
       if(y >= highest){
    bigBoy = x;
    highest = y;

   }   
        i++;
    c.println("enter the name of gamer #"+i);
   x = c.readString();
    c.println("What is your score "+x+"?");
   y = c.readInt();
   highest = y;
   lowest = y;


   if(y <= lowest){
   lilBoy = x;
   lowest = y;
   }


    if(i == a) break;


    }

    c.println("The highest is "+lilBoy);
    c.println("The lowest is "+bigBoy);

  }
}
  • 2
    Possible duplicate of [Finding the max/min value in an array of primitives using Java](https://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-of-primitives-using-java) – ZektorH Nov 25 '19 at 16:48

1 Answers1

1

You are very close and on the right track. You should initialize highest to Integer.MIN_VALUE and lowest to Integer.MAX_VALUE. Then when you find one lower or higher, reset those values to the newer ones and continue until no more input.

Continue reading in values and names, setting appropriate values until you're finished. Then print the results.

Here is a sample.


if (val > highest) {
   highest = val;
   highname = currentName;
}

Also, instead of having a separate variable i for determining when to stop, you can do the following:

while (a-- > 0) {
  // rest of your code here.
}
WJS
  • 36,363
  • 4
  • 24
  • 39