简体   繁体   English

如果还有错误,我不理解此错误

[英]If else error I don't understand this error

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 我不明白此错误是逻辑错误还是其他问题,此处是编码“此程序应将里亚尔转换为用户使用多路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 我正在使用的程序是Jcreator

You aren't marking the start and end the bodies of your if statements - wihch means they're just single statements. 您没有在if语句的主体的开始和结尾处标记-wihch意味着它们只是单个语句。 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. 如您所见, else不“属于”任何if块。 I'd strongly recommend that you always use braces, even for single-statement if bodies: 我强烈建议您始终使用大括号,即使对于单声明, if正文:

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. 请注意,如果您使用IDE来格式化代码,则所有这些都将变得更加清晰。 The indentation that the IDE will apply will show you how things are actually being understood... IDE将应用的缩进将向您显示事物的实际理解方式...

You are supposed to use curly braces if your if blocks have more than a single statement : 如果您的if块包含多个语句,则应该使用花括号:

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"); 
} ... and so on ...

When you write it without the braces, it's equivalent to: 当您不使用大括号编写它时,它等效于:

if(opp==1) {
    c=riyal*0.267;
}
System.out.print(riyal+" Riyals is converted to "+c" Dollars");

else if (opp==2)

so the else if is not related to any if . 所以else ifif无关。

if , else , and other flow control statements operate on the one statement that follows them. ifelse和其他流控制语句对它们之后的一个语句进行操作。 When you have more than one, you use a block ( {} ): 如果有多个,则使用一个块( {} ):

if(opp==1) {
    c=riyal*0.267;
    System.out.print(riyal+" Riyals is converted to "+c" Dollars");
}
else if (opp==2) {
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM