I'm pretty good with Visual Basic, but I'm a total beginner with Java. I get two or three errors claiming that my two of my elses have no corresponding ifs, but I can't figure out how to get the syntax correct. I'd really appreciate it if anyone could have a look and let me know what's wrong ...
class CISP300ASGN12 {
public static void main(String[] args) {
int num = 0, num2 = 0, num3 =0, big = 0, small = 0;
System.out.print(" Enter the First Number: ");
num1 = Keyboard.readInt();
System.out.print(" Enter the Second Number: ");
num2 = Keyboard.readInt();
System.out.print(" Enter the Third Number: ");
num3 = Keyboard.readInt();
if (num1 > num2) {
if (num1 > num3)
big = num1;
if (num3 < num2)
small = num3;
else
small = num2;
else
big = num3; small = num2;
}
else
if (num2 > num3){
big = num2;
if (num1 < num3)
small = num1;
else
small = num3;
else
big = num3; small = num1;
}
System.out.print("The Largest Number is: %s", big);
System.out.print("The Smallest Number is: %s", small);
}
}
Comments
Get in the habit of indenting better. That will avoid a lot of these bracket balancing or block matching errors.
The code below compiles but you'll have to figure out if it behaves as you intended at run time.
[code]
import java.util.Scanner;
public class CISP300ASGN12 {
public static void main(String[] args)
{
int num1 = 0, num2 = 0, num3 =0, big = 0, small = 0;
Scanner Keyboard = new Scanner(System.in);
System.out.print(" Enter the First Number: ");
num1 = Keyboard.nextInt();
System.out.print(" Enter the Second Number: ");
num2 = Keyboard.nextInt();
System.out.print(" Enter the Third Number: ");
num3 = Keyboard.nextInt();
if (num1 > num2)
{
if (num1 > num3)
{
big = num1;
if (num3 < num2)
small = num3;
else
small = num2;
}
else
{
big = num3;
small = num2;
}
}
else
{
if (num2 > num3){
big = num2;
if (num1 < num3)
small = num1;
else
small = num3;
}
else
{
big = num3;
small = num1;
}
}
System.out.print("The Largest Number is: "+big);
System.out.print("The Smallest Number is: "+small);
} // end main method
}
[/code]