I need to write a java algorithm that receive 5 numbers from type int and print the max and min by using only 6 times with the Conditional Exchange:
If (x>y) {
Int tmp = x;
x=y;
y=x;
}
The problem is that I only manage to do this algorithm with 7 conditional exchange and not 6. Can someone help me to understand what I'm missing?
Scanner myScanner = new Scanner(System.in);
int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt();
if(a>b)
{
int tmp = b;
b = a;
a = tmp;
}
if(a>c)
{
int tmp = c;
c = a;
a = tmp;
}
if(a>d)
{
int tmp = d;
d = a;
a = tmp;
}
if(a>e)
{
int tmp = e;
e = a;
a = tmp;
}
if(b>e)
{
int tmp = e;
e = b;
b = tmp;
}
if(c>e)
{
int tmp = e;
e = c;
c = tmp;
}
if(d>e)
{
int tmp = e;
e = d;
d = tmp;
}
System.out.println(a);
System.out.println(e);
maxis inaore? (I will say no more, as that would risk you committing plagiarism.)