Results 1 to 2 of 2
  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default printing the highest and lowest of three numbers using java

    Hello, I am working on an assignment but I am not getting any out put and I couldn't fix it, can any one help me what my problem is?
    thank you.
    The class HighLow below asks for three integers and prints the highest and lowest of them on screen. Your task is to write the missing methods high and low, which receives the integers user inputs as parameters and return the highest and lowest integers respectively.



    import java.util.Scanner;



    public class HighLow {

    public static void main(String[] args) {

    int number1, number 2, number 3, high, low;

    Scanner reader = new Scanner(System.in);



    System.out.print("Type in the first integer: ");

    number 1 = reader.nextInt();



    System.out.print("Type in the second integer: ");

    number 2 = reader.nextInt();



    System.out.print("Type in the third integer: ");

    number 3 = reader.nextInt();



    high = high(number1, number2, number3);

    low = low(number1, number2, number3);



    System.out.print("Highest number was " + high);

    System.out.print(" and the lowest was" + low + "\n");

    }



    // Declarations of the methods high() and low()



    }



    Note: Above code is not to be copied to the return field. Write only the declarations of the missing methods there.
    Example output


    Type in the first integer: 5
    Type in the second integer: 2
    Type in the third integer: 9
    Highest number was 9 and the lowest was 2
    Java Code:
    import java.util.Scanner;
    
     
    
    public class HighLow {
    
        public static void main(String[] args) {
    
            int number1, number2, number3, high, low;
    
            Scanner reader = new Scanner(System.in);
    
     
    
            System.out.print("Type in the first integer: ");
    
            number1 = reader.nextInt();
    
     
    
            System.out.print("Type in the second integer: ");
    
            number2 = reader.nextInt();
    
     
    
            System.out.print("Type in the third integer: ");
    
            number3 = reader.nextInt();
    
     
    
            high = high(number1, number2, number3);
    
            low = low(number1, number2, number3);
    
     
    
            System.out.print("Highest number was " + high);
    
            System.out.print(" and the lowest was" + low + "\n");
    
        }
    
     
    
    //  Declarations of the methods high() and low()
        private static int high(int number1, int number2, int number3){
        	int high=0;
        
        for(int i=1; i<=3; i++){
        	Scanner reader = new Scanner(System.in);
        	System.out.print("Type in the first integer: ");
            number1 = reader.nextInt();
            System.out.print("Type in the first integer: ");
            number1 = reader.nextInt();
            System.out.print("Type in the third integer: ");
            number3 = reader.nextInt();
            if(number1>number2 && number1>number3)
            	high=number1;
            if(number2>number1 && number2>number3)
            	high=number2;
            if(number3>number1 && number3>number2)
            	high=number3;
            
        	}return high;
        }
        
        private static int low(int number1, int number2, int number3){
        	int low=0;
        for(int i=1; i<=3; i++){
        	Scanner reader = new Scanner(System.in);
        	System.out.print("Type in the first integer: ");
            number1 = reader.nextInt();
            System.out.print("Type in the first integer: ");
            number1 = reader.nextInt();
            System.out.print("Type in the third integer: ");
            number3 = reader.nextInt();
            if(number1<number2 && number1<number3)
            	low=number1;
            if(number2<number1 && number2<number3)
            	low=number2;
            if(number3<number1 && number3<number2)
            	low=number3;
        	}
                 return low;
        }
    
     
    
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: printing the highest and lowest of three numbers using java

    You are passing number1, number2 and number3 as parameters to the methods; you have just read their values in the main( ... ) method, so there is no need to read those values again in the methods. Those methods should do one thing: determing the minimum/maximum value of those three numbers; skip all that reading stuff in the methods.

    edit: and those loops in the methods don't make sense at all ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Get highest and lowest
    By johnmarkquiriones in forum New To Java
    Replies: 2
    Last Post: 11-26-2013, 01:59 PM
  2. Getting/Printing the Highest to lowest
    By micolord in forum New To Java
    Replies: 12
    Last Post: 09-14-2011, 04:44 PM
  3. Replies: 4
    Last Post: 02-15-2011, 11:05 PM
  4. cant get highest and lowest to display
    By tracey in forum New To Java
    Replies: 2
    Last Post: 05-16-2010, 08:48 AM
  5. Replies: 21
    Last Post: 11-05-2008, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •