Reputation: 25
Hello I'm trying to make a code that takes three integers from the command line and sorts them into the min, mid, and max values. I can't figure out the mid programming. It won't always sort them properly. Can you help?
public class SortInteger{
public static int max3(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
public static int min3(int a, int b, int c) {
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;}
public static int sort(int a, int b, int c){
int sort = a;
if (sort > b && sort < c) sort = a;
else sort = b;
if (sort > a && sort < c) sort = b;
else sort =c;
if (sort > c && sort < a) sort = c;
else sort =b;
if (sort > c && sort < b) sort = c;
else sort = b;
if (sort > a && sort < b) sort = c;
else sort = c;
return sort;
}
public static void main(String[] args){
int a= Integer.parseInt(args [0]);
int b=Integer.parseInt(args[1]);
int c=Integer.parseInt(args[2]);
StdOut.println("Min is " + min3(a, b, c));
StdOut.println("Mid is " + sort(a, b, c));
StdOut.println("Max is " + max3(a, b, c));
}
}
Upvotes: 1
Views: 329
Reputation: 720
Try the following:
public class SortInteger
{
//use general sorting algorithm for arbitrary length, this is for 3 length specifically
public static int[] sort(int[] inputs)
{
int k;
if(inputs[0] >= inputs[1])
{
k = inputs[0];
inputs[0] = inputs[1];
inputs[1] = k;
}
if(inputs[1] >= inputs[2])
{
k = inputs[1];
inputs[1] = inputs[2];
inputs[2] = k;
}
//incase our last element is less than our first we repeat:
if(inputs[0] >= inputs[1])
{
k = inputs[0];
inputs[0] = inputs[1];
inputs[1] = k;
}
return inputs
}
public static void main(String[] args)
{
int[] x = new int[3];
x[0] = Integer.parseInt(args[0]);
x[1] = Integer.parseInt(args[1]);
x[2] = Integer.parseInt(args[2]);
x = sort(x);
System.out.println("min is: " + x[0]);
System.out.println("mid is: " + x[1]);
System.out.println("max is: " + x[2]);
}
}
Upvotes: 0
Reputation: 1371
Try:
public static int mid(int a, int b, int c){
return a + b + c - max(a,b,c) - min(a,b,c);
}
Also for the min
and max
just use Math
:
public static int min(int a, int b, int c){
return Math.min(Math.min(a,b),c);//Replace with Math.max for max.
}
Upvotes: 4
Reputation: 2397
You're stepping all over your own toes inside the sort function. Take, for instance, your first two if statements:
if (sort > b && sort < c) sort = a;
else sort = b;
if (sort > a && sort < c) sort = b;
else sort =c;
If a is between b and c, your first if statement will be true, and sort will be kept as the value of a. But, then consider your next one. The value in a will not be greater than a, so the second if statement will be false, and change sort to c, even though you already found a to be the middle value. Not what you wanted. To fix this, you could change the code you execute when your if statements are true to just return the value of sort. So, like:
if (sort > b && sort < c) return sort;
else sort = b;
if (sort > a && sort < c) return sort;
else sort =c;
// etc.
Upvotes: 1