• Post Reply Bookmark Topic Watch Topic
  • New Topic

printing the highest and lowest of three numbers using java  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Why do the "high" and "low" methods ask for input from the user? All input is done in the main method, the other two methods just need to calculate their output. Also, what is the point of the loops in those methods?
 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thank you! this is my first java program on declaration and calling methods. As you suggested me I
did some adjustments to the code and now it is working.
once again thank you!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I don't think the code works correctly if two (or all three) number are identical.
 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
yes you are right, it doesn't work if two or three of the numbers are identical but
I added "=" in all the if clauses and now it works fine.
But thank you though!
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
daniel kidanee wrote:yes you are right, it doesn't work if two or three of the numbers are identical but...

You might also want to have a look at Math.min() and Math.max(). They only take two arguments, but you could easily use 'em to return the result for three.

Winston
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your solution may work, but it is not ideal. Think how you'd have to change it if you needed to input four numbers. or eight. or 100...

As an exercise, can you think of a better way to solve it than to explicitly write out the code to test every possible combination?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!