How do I determine the highest and lowest value in user-entered input?

后端 未结 4 1944
我寻月下人不归
我寻月下人不归 2020-12-11 14:43

I\'m trying to get the highest and lowest numbers entered by the user. The code below seems to be mostly working but I can\'t seem to get the right number for the lowest val

相关标签:
4条回答
  • 2020-12-11 14:43
    import java.io.*;   
    public class jem3
    {
        public static void main(String []args)
        {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in ));     
        int high;
        int lowest;
        int num;
        System.out.println("Enter number");
    
        num=Integer.parseInt(dataIn.readLine());
        lowest = highest = num;
    
        for(int a=0;a<10;a++)
        {
            try
    
                {
    
                    num=Integer.parseInt(dataIn.readLine());
                }
                catch(IOException e)
                {
                    System.out.println("error");
                }
    
    
                if(num>high)
                {
                    high=num;
                }
                if(num<lowest)
                {
                    lowest=num;
                }
    
        }
        System.out.println("highest is:"+  high);
        System.out.println("lowest is: "+lowest);
    
    
        }
    }
    

    i added code after reeading first line to set high and lowest, so if user will give you 10 numbers '9' it will output lowest number being '9' and not 1 as it would (similiar with high).

    0 讨论(0)
  • 2020-12-11 14:49

    you are on the right track for capturing highest. you need to apply similar logic for lowest.

    0 讨论(0)
  • 2020-12-11 14:58

    What is the purpose of A? You're not modifying lowest either. You're close, try this:

    num = ...
    if ( num > max ) max = num;
    if ( num < min ) min = num;
    
    System.out.println("Highest: " + max);
    System.out.println("Lowest: " + min);
    
    0 讨论(0)
  • 2020-12-11 15:08
    import java.io.*;
    
    public class jem3{
    
      public static void main(String []args){
      BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));        
        int high=0;
        int lowest=0;
        int num=0;
        boolean test = true;
    
        System.out.println("Enter number");
    
        for(int a=0;a<10;a++)
        {
            try
            {
                num=Integer.parseInt(dataIn.readLine());
            }
            catch(IOException e)
            {
                System.out.println("error");
            }
    
            if(num>high)
            {
                high=num;
            }
            //add an initial value to 'lowest'
            if (test)
                lowest = num;
                test = false;
    
            if(num < lowest)
            {
                lowest = num;
            }                        
        }
    
        System.out.println("highest is:"+  high);
        System.out.println("lowest is: "+ lowest);
        }
    }
    
    0 讨论(0)
提交回复
热议问题