...

View Full Version : Newbie help with if/else statments



cpole
01-09-2012, 02:41 AM
I am stuck on a homework problem. I think I am on the right track but am getting stuck on the if/else statement. I could be totally wrong though.

Here is my assignment: Write a program that reads three integer inputs into variables. Display the input values in both the ordered entered and in sorted order. This program should sort the numbers so that value1 <= value2 <= value3.

I have only been learning Java for a couple weeks, any help would be appreciated. Hoping to make this a career one day.

Here is the code:


import java.util.Scanner;

public class OrderVariable {
public static void main (String [] args) {

Scanner input = new Scanner (System.in);

System.out.prntln("Enter first variable");
int VariableOne = input.nextInt();

System.out.prntln("Enter second variable");
int VariableTwo = input.nextInt();

System.out.prntln("Enter third variable");
int VariableThree = input.nextInt();

if (VariableOne > VariableTwo && VariableOne > VariableThree)
int Biggest = VariableOne;

else if (VariableTwo > VariableOne && VariableTwo > VariableThree)
int Biggest = VariableTwo;

else (VariableThree > VariableOne && VariableThree > VariableTwo)
int Biggest = VariableThree;

if (VariableOne < VariableTwo && VariableOne < VariableThree)
int Smallest = VariableOne;

else if (VariableTwo < VariableOne && VariableTwo < VariableThree)
int Smallest = VariableTwo;

else (VariableThree < VariableOne && VariableThree < VariableTwo)
int Smallest = VariableThree;


if (VariableOne < Biggest && VariableOne > Smallest){
int Mid = VariableOne;

else if (VariableTwo < Biggest && VariableTwo > Smallest)
int Mid = VariableTwo;

else (VariableThree < Biggest && VariableThree > Smallest)
int Mid = VariableThree;

System.out.println(VariableOne + ", " + VariableTwo + ", " + VariableThree);
System.out.println(Max + ", " + Mid + ", " + Min);
}
}

Fou-Lu
01-09-2012, 02:50 PM
Have you learned the use of arrays? If so, this is far more trivial than this:


int[] iArray = new int[3];
iArray[0] = input.nextInt();
iArray[1] = input.nextInt();
iArray[2] = input.nextInt();

System.out.println(iArray[0] + ", " + iArray[1] + ", " + iArray[2]);
Arrays.sort(iArray);
System.out.println(iArray[0] + ", " + iArray[1] + ", " + iArray[2]);


Otherwise, if/elseif's would work fine. You have two problems above; the first is that else's cannot carry conditions. The second is that you have an opening brace where it doesn't belong. IMO you should brace every branch even if its only 1 instruction. And finally, your accessing variables that don't exist. You used Biggest and Smallest, not Max and Min.
Another approach would be to use a more simplified drill:


if (v1 >= v2)
{
if (v1 >= v3)
{
// first is larger than both the second and the third
biggest = v1;
if (v2 >= v3)
{
// third is smaller then the second, so therefore third is the smallest
smallest = v3;
mid = v2;
}
else
{
smallest = v2;
mid = v3;
}
}
else
{
biggest = v3;
smallest = v2;
mid = v1;
}
}
else if (v2 >= v3)
{
// With the first if, we know that v2 now must be the biggest
biggest = v2;
if (v1 >= v3)
{
smallest = v3;
mid = v1;
}
else
{
smallest = v1;
mid = v3;
}
}
else
{
// Since neither v1 or v2 is the largest, we know that v3 is the largest:
biggest = v3;
if (v1 >= v2)
{
smallest = v2;
mid = v1;
}
else
{
smallest = v1;
mid = v2;
}
}


Hmm, yep that looks like it would work too.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum