We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Highest number, Lowest and their difference

My homework goes like this:
Write a program that accepts as many integers the user wants to input. Determine the highest and lowest numbers and subtract all other numbers from the highest number.
example: numbers to be entered: 6
number1 :15
number2:20
number3:29
number4:38
number5:12
number6: 42

Output:
Highest: 42
Lowest:12
[The difference from the highest is:
[42-15]=27
[42-20]=22 <---how can i display them?
[42-29]=13
[42-38]=4
[42-312]=30

.. please help me.thanks

2
Contributors
10
Replies
1 Hour
Discussion Span
2 Months Ago
Last Updated
28
Views
Question
Answered
JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
import java.util.*;

   public class HighestLowestDifference2324 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double maxValue = Double.MIN_VALUE;
        double minValue = Double.MAX_VALUE;  
        double minValue1 = Double.MAX_VALUE; 
        double minValue2 = Double.MAX_VALUE; 

        int count = 0;
          double difference = 0; 

        System.out.println("Enter numbers. Terminate with Q.");
        while (input.hasNextDouble()) { 

            double Value = input.nextDouble();

            if (Value > maxValue) {
                maxValue = Value;
            }
            if (Value < minValue) {
                minValue = Value;
            }
            if (Value < minValue) {
                minValue1 = Value; }
                if (minValue == minValue1)   {
                minValue = Value; }
            if (Value < minValue1) {
                minValue2 = Value; }
                if (minValue1 == minValue2)   {
                minValue1 = Value; }
            }
                count++;  
                difference = maxValue-minValue;        
                {
        if (count > 0) {                                          
            System.out.println("Highest no. is: "  + maxValue);
            System.out.println("Lowest no. is: "  + minValue);
                System.out.println("The difference from the highest is: \n" + difference);
        }
           else {
            System.out.println("Have a good day!"); }
    }
}
}
JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

that is my code... pls. help me. I can't show all the differences that I want to display.

JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

What exactly is your question? - the code you posted displays the results.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

I have to get the difference of the highest number and the rest..
for example, to get the differences: 27,22,13,4,30. when i use the code , it only shows 27.

JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Logically you don't know the highest number until all the numbers have been input, so to show all the differences you will have to store all the numbers (eg in an array) as they are input. Then, when you know the largest, you can loop through the array and disaply all the differences.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

so I have to change all my codes into an array?.. but is it possible that an array contains numbers yet
to be input ?

JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

... only if you have time travel!
You start with an array that's big enough, fill in the data and keep track of how many data were entered ( = how many of the array elements have been used)

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

SIR JAMES... i'M SORRY FOR ASKING SILLY QUESTIONS ..xD. can you pls. add that to my code and show it off?...
i dont't know how to do array, eh?..please. ^_^ –

JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Sorry, but no. I'm no going to write your homework for you. Your teacher obviously thinks that you should know enough about arrays, or are ready to learn enough, so that's what you should do. Here a good place to start:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
It's up to you to try your best. There are plenty of people here who will help you if you do.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30
Question Answered as of 2 Months Ago by JamesCherrill

Ok. I understand. Thank you by the way.. ^^

JavaGr33nh0rn
Light Poster
25 posts since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0862 seconds using 2.77MB