I don't understand this error is it logical error or something else here is the coding " this program should convert riyal to currency that is chosen by the user using multiway if-else
import java.util.Scanner;
public class Convert
{
public static void main (String[] args)
{
Scanner kb= new Scanner (System.in);
double riyal, c;
int opp;
System.out.println("Please enter the amount of money in Riyal: ");
riyal= kb.nextDouble();
System.out.println("Enter the currency option(1- Dollar, 2- Euro, 3- Yen): ");
opp= kb.nextInt();
if(opp==1)
c=riyal*0.267;
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
else if (opp==2)
c=riyal*0.197;
System.out.print(riyal+" Riyals is converted to "+c" Euros");
else if (opp==3)
c=riyal*0.27.950;
System.out.print(riyal+" Riyals is converted to "+c" Yens");
else
{
System.out.println("Invalied opption");
}
}
}
Error message:
error: illegal start of expression
error: 'else' without 'if'
error: ';' expected
error: ')' expected
The program that I'm using is Jcreator
You aren't marking the start and end the bodies of your if
statements - wihch means they're just single statements. You've currently effectively got:
if(opp==1) {
c=riyal*0.267;
}
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
else if (opp==2)
As you can see, that else
doesn't "belong" to any if
block. I'd strongly recommend that you always use braces, even for single-statement if
bodies:
if(opp==1) {
c=riyal*0.267;
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
} else if (opp==2) {
...
} // etc
Note that if you get your IDE to format your code, this should all become rather clearer. The indentation that the IDE will apply will show you how things are actually being understood...
See more on this question at Stackoverflow