Name: Anonymous 2011-10-17 0:03
Hey guys I need help with my java coding. I'm required to write a code that takes 3 numbers (user input) and sort them from smallest to largest. Can someone show me how to code this?
import java.util.Scanner;
public class anus
{
static Scanner s = new Scanner(System.in);
public static void main(String args[])
{
int[] a = { getInt() },
b = { getInt() },
c = { getInt() };
if(b[0] > a[0])
swap(a,b);
if(c[0] > a[0])
if(c[0] > b[0]){
swap(a,c);
if(c[0] > b[0])
swap(b,c);
}
else
swap(b,c);
System.out.printf("[%d][%d][%d]%n",a[0],b[0],c[0]);
}
public static int getInt()
{
System.out.printf("Enter a integer: ");
return s.nextInt();
}
public static void swap(int[] a,int[] b)
{
a[0] ^= b[0];
b[0] ^= a[0];
a[0] ^= b[0];
}
}