If else error 我不明白这个错误
If else error I don't understand this error
我不明白这个错误是逻辑错误还是其他什么是编码“这个程序应该将里亚尔转换为用户使用 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: illegal start of expression
error: 'else' without 'if'
error: ';' expected
error: ')' expected
我使用的程序是Jcreator
如果你的 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 ...
不带大括号的写法相当于:
if(opp==1) {
c=riyal*0.267;
}
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
else if (opp==2)
所以 else if
与任何 if
无关。
if
、else
和其他流程控制语句对它们后面的 one 语句进行操作。当你有多个时,你使用一个块({}
):
if(opp==1) {
c=riyal*0.267;
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
}
else if (opp==2) {
// ...
您没有标记 if
语句的开始和结束主体 - 这意味着它们只是单个语句。您目前有效获得:
if(opp==1) {
c=riyal*0.267;
}
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
else if (opp==2)
如您所见,else
不会 "belong" 任何 if
块。我强烈建议您 始终 使用大括号,即使是单语句 if
正文:
if(opp==1) {
c=riyal*0.267;
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
} else if (opp==2) {
...
} // etc
请注意,如果您让 IDE 格式化您的代码,这一切都会变得更加清晰。 IDE 将应用的缩进将向您展示事情 实际上 是如何被理解的...
我不明白这个错误是逻辑错误还是其他什么是编码“这个程序应该将里亚尔转换为用户使用 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: illegal start of expression
error: 'else' without 'if'
error: ';' expected
error: ')' expected
我使用的程序是Jcreator
如果你的 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 ...
不带大括号的写法相当于:
if(opp==1) {
c=riyal*0.267;
}
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
else if (opp==2)
所以 else if
与任何 if
无关。
if
、else
和其他流程控制语句对它们后面的 one 语句进行操作。当你有多个时,你使用一个块({}
):
if(opp==1) {
c=riyal*0.267;
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
}
else if (opp==2) {
// ...
您没有标记 if
语句的开始和结束主体 - 这意味着它们只是单个语句。您目前有效获得:
if(opp==1) {
c=riyal*0.267;
}
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
else if (opp==2)
如您所见,else
不会 "belong" 任何 if
块。我强烈建议您 始终 使用大括号,即使是单语句 if
正文:
if(opp==1) {
c=riyal*0.267;
System.out.print(riyal+" Riyals is converted to "+c" Dollars");
} else if (opp==2) {
...
} // etc
请注意,如果您让 IDE 格式化您的代码,这一切都会变得更加清晰。 IDE 将应用的缩进将向您展示事情 实际上 是如何被理解的...