Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the biggest number among 3, I ran into problems for the last part as bolded

import java.util.Scanner;
class CurrencyExchange_1 {
 
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print("Input any 3 numbers");
    int firstnumber = sc.nextInt();
    int secondnumber = sc.nextInt();
    int thirdnumber = sc.nextInt();

    //Finding the bigger number
    if (firstnumber > secondnumber);
    if (firstnumber > thirdnumber ) {
        System.out.println("Biggest number is " +firstnumber);

    } else if (secondnumber > firstnumber);
    if (secondnumber > thirdnumber); {
        System.out.println("Biggest number is " +secondnumber);

    } **else if** (thirdnumber > firstnumber);
    if (thirdnumber > secondnumber); {
        System.out.println("Biggest number is " +thirdnumber);
    }
  }
}

For the bolded part, I originally wrote < else > only without the conditions but it was an error. Even with the code as written above, an error prompts for <'else' without 'if'>. I am not too sure where went wrong as when I completely delete the last < "else" > part, the code works perfectly. I was reading answers for other questions that <else {...}> can work perfectly when not all conditions are met but it seems to be incorrect for me. Please pardon my complicated code because I am currently learning the basics and I am required to solve it using what I know currently which is just the comparing part of programming.

Also, is it me or there is a lot of auto-correct going on? Some words keep changing back when I have clearly edited them!

Thank you all for your kindness! I finally understand my errors! Would like to thank each of y'all personally but to prevent clutter, i'm going to write it here!

like image 937
Javanewbie Avatar asked Jan 29 '26 10:01

Javanewbie


2 Answers

I would suggest you use the && statement in Java's if.

By writing if(condition1 && condition2) both condition1 and condition2 must be true for the execution to enter the if.

Now you can easily rewrite your code to

import java.util.Scanner;
class CurrencyExchange_1 {
 
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print("Input any 3 numbers");
    int firstnumber = sc.nextInt();
    int secondnumber = sc.nextInt();
    int thirdnumber = sc.nextInt();

    //Finding the bigger number
    if (firstnumber > secondnumber && firstnumer > thirdnumber) {
        System.out.println("Biggest number is " +firstnumber);
    } else if (secondnumber > firstnumber && secondnumber > thirdnumber) {
        System.out.println("Biggest number is " +secondnumber);
    } else
        System.out.println("Biggest number is " +thirdnumber);
    }
  }
}

To answer your actual question the reason why you can not use the else statement is because you write if(...); {} and also else if(...); . In java you do not write a line terminator ; in if statements. Just write if(...) {logic}

By writing if(...); or if(...){}there will be noting to execute if the condition is true hence the if statement is useless. There should always be some logic that is to be executed if the condition in the if statement is met.

like image 54
MarkusAnd Avatar answered Jan 31 '26 22:01

MarkusAnd


Your code is having incorrect if statements e.g if (firstnumber > secondnumber); if this if condition is true the there is no logic to execute.

You can do simply following to what you want to achieve. See complete working code here:

int largest = (firstnumber > secondnumber) ?
            (firstnumber > thirdnumber ? firstnumber : thirdnumber) :
            (secondnumber > thirdnumber ? secondnumber : thirdnumber);

UPDATED Following the simplify form of above logic using if-else:

int largest = thirdnumber;
if(firstnumber > secondnumber)
{
    if(firstnumber > thirdnumber)
        largest = firstnumber;
}
else if (secondnumber > thirdnumber)
    largest = secondnumber;

Following is the complete code. See it working here:

public static void main (String[] args)
{
    try( Scanner sc = new Scanner(System.in) )
    {
        System.out.print("Input any 3 numbers:");
        int firstnumber = sc.nextInt();
        int secondnumber = sc.nextInt();
        int thirdnumber = sc.nextInt();

        int largest = thirdnumber;
        if(firstnumber > secondnumber)
        {
            if(firstnumber > thirdnumber)
                largest = firstnumber;
        }
        else if (secondnumber > thirdnumber)
            largest = secondnumber;

        System.out.println("Biggest number is: " +largest);
    }
}
like image 20
cse Avatar answered Jan 31 '26 23:01

cse