public class MinMax { public static void main(String args[]) { //Worked with Zach on this and SortNumbers double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); double z = Double.parseDouble(args[2]); if(x < 1  x > 10) { System.out.println("Your first number is not between 1 and 10."); } else if(y < 1  y > 10) { System.out.println("Your second number is not between 1 and 10."); } else if(z < 1  z > 10) { System.out.println("Your third number is not between 1 and 10."); } else if(x >= y && y >= z) { System.out.println(x + " " + y + " " + z); System.out.println(x + " is the largest number and " + z + " is the smallest number."); } else if(x >= y && z >= y) { System.out.println(x + " " + y + " " + z); System.out.println(x + " is the largest number and " + y + " is the smallest number."); } else if(y >= x && x >= z) { System.out.println(x + " " + y + " " + z); System.out.println(y + " is the largest number and " + z + " is the smallest number."); } else if(y >= z && z >= x) { System.out.println(x + " " + y + " " + z); System.out.println(y + " is the largest number and " + x + " is the smallest number."); } else if(z >= y && x >= y) { System.out.println(x + " " + y + " " + z); System.out.println(z + " is the largest number and " + y + " is the smallest number."); } else if(z >= x && y >= x) { System.out.println(x + " " + y + " " + z); System.out.println(z + " is the largest number and " + x + " is the smallest number."); } } }
16 Replies  1379 Views  Last Post: 12 October 2012  06:49 PM
#1
Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  02:19 PM
Replies To: Need Help Creating Code to Find Min and Max Numbers
#2
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  02:36 PM
Once you have the numbers in an array, you use a for loop to go through every element and it should be easy to get the min and max at that point.
#3
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  02:48 PM
#4
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  03:07 PM
However, you really don't need to do that to just get the max and the min. Here's a hint to simplify your life: at any given point, you've seen exactly one max value and one min value.
#5
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  03:41 PM
#6
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  03:44 PM
This post has been edited by Kakerergodt: 12 October 2012  05:00 PM
#7
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  04:17 PM
Could I do something like this:
for(a >= b && a >= c && a >= d && a >= e) { System.out.println(a + " is the max"); } for(b >= a && b >= c && b >= d && b >= e) { System.out.println(b + " is the max"); } etc.
Then for the minimum, I could have something like:
for(a <= b && a <= c && a <= d && a <= e) { System.out.println(a + " is the min"); } etc.
Will this work for giving a minimum and a maximum? I know it's not exactly the way the hints were given, but it does work, no?
#8
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  04:30 PM
public class MinMax5 { public static void main(String args[]) { double a = Double.parseDouble(args[0]); double b = Double.parseDouble(args[1]); double c = Double.parseDouble(args[2]); double d = Double.parseDouble(args[3]); double e = Double.parseDouble(args[4]); if(a < 1  a > 10) { System.out.println("Your first number is not between 1 and 10."); } else if(b < 1  b > 10) { System.out.println("Your second number is not between 1 and 10."); } else if(c < 1  c > 10) { System.out.println("Your third number is not between 1 and 10."); } else if(d < 1  d > 10) { System.out.println("Your fourth number is not between 1 and 10."); } else if(e < 1  e > 10) { System.out.println("Your fifth number is not between 1 and 10."); } else { while(a >= b && a >= c && a >= d && a >= e); { System.out.println(a + " is the max"); } } } }
#9
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  04:43 PM
#10
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  04:59 PM
You've got two choices now, either look into array and make it work with a loop. Or skip both and do something like this:
<Pseudocode> double maxValue = a; //assume the first is the biggest double minValue = a; //assume the first is the smalles  check whether the next value is greater than the current maxValue, if so, update maxValue.  Do the same for minValue.  repeat for b,c,d,e
(The last two steps would fit nicely in a loop, since you want to do it for all the values you have, without a loop you'll have to repeat it as many times as you have numbers.)
Edit:
If you want to use a loop a forloop would be the most logical to use.
<Pseudocode> double[] array = {a, b, c, d, e}; //Add all elements to array double maxValue = array[0]; //assume the first is the biggest double minValue = array[0]; //assume the first is the smalles for( all values ) {  check whether the next value is greater than the current maxValue, if so, update maxValue.  Do the same for minValue. } System.out.println(maxValue + " is max. " + minValue + " is min.");
This post has been edited by Kakerergodt: 12 October 2012  05:06 PM
#11
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  05:24 PM
#12
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  05:33 PM
public class MinMax5 { public static void main(String args[]) { double a = Double.parseDouble(args[0]); double b = Double.parseDouble(args[1]); double c = Double.parseDouble(args[2]); double d = Double.parseDouble(args[3]); double e = Double.parseDouble(args[4]); double maxValue = a; double minValue = a; if(a < 1  a > 10  b < 1  b > 10  c < 1  c > 10  d < 1  d > 10  e < 1  e > 10) { System.out.println("Please enter numbers that are only between 1 and 10."); } //start max value calculations else if (a >= b && a >= c && a >= d && a >= e) { maxValue = a; System.out.println(maxValue + " is the max."); } else if (b >= a && b >= c && b >= d && b >= e) { maxValue = b; System.out.println(maxValue + " is the max."); } else if (c >= a && c >= b && c >= d && c >= e) { maxValue = c; System.out.println(maxValue + " is the max."); } else if (d >= a && d >= b && d >= c && d >= e) { maxValue = d; System.out.println(maxValue + " is the max."); } else if (e >= a && e >= b && e >= c && e >= d) { maxValue = e; System.out.println(maxValue + " is the max."); } //end max value calculations //start min value calculations if (a <= b && a <= c && a <= d && a <= e) { minValue = a; System.out.println(minValue + " is the min."); } else if (b <= a && b <= c && b <= d && b <= e) { minValue = b; System.out.println(minValue + " is the min."); } else if (c <= a && c <= b && c <= d && c <= e) { minValue = c; System.out.println(minValue + " is the min."); } else if (d <= a && d <= b && d <= c && b <= e) { minValue = d; System.out.println(minValue + " is the min."); } else if (e <= a && e <= b && e <= c && e <= d) { minValue = e; System.out.println(minValue + " is the min."); } //end min value calculations } }
#13
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  05:42 PM
If you first set maxValue to a and then compare maxValue to b, then there's no use in comparing c,d,e to a or b, because you know maxValue holds the largest no matter what.
double maxValue = a; //assume the first is the biggest double minValue = a; //assume the first is the smalles if(maxValue < B)/> maxValue = b; if(maxValue < c) maxValue = c; if(maxValue < d) maxValue = d; if(maxValue < e) maxValue = e; System.out.println(maxValue + " is max.");
Edit: You'll probably figure out how to do the same with minValue.
This post has been edited by Kakerergodt: 12 October 2012  05:44 PM
#14
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  05:54 PM
#15
Re: Need Help Creating Code to Find Min and Max Numbers
Posted 12 October 2012  06:30 PM
For example, I would enter:
java SortNumbers5 7 3 4 2 9
Then, if the code worked properly, it would put them in order.
