If / Else语句出错

时间:2013-10-12 18:31:28

标签: java if-statement

public class proj22
{
    public static void main(String[] args)
    {   float degreef, degreec, degree;
            string cel = "c";
            string far = "f";
            string unit;
            System.out.println("Enter a temperture:");
            scanner keyboard = new Scanner (System.in);
            degree = keyboard.nextFloat();
            System.out.println("Enter a c to designate Celsius, or an f to designate..");        
            unit = keyboard.next();
            if (unit.equalsIgnoreCase(cel)||unit.equalsIgnoreCase(far))
            {
                    if (unit.equalsIgnoreCase(cel))
                    {
                            degreec = 5*(degreef-32)/9;
                            System.out.println("The degree you entered in Celsius is" + 
                            System.out.println("The degree you entered in Fahrenheit" + 

                    }
                    else
                            degreef = degree;
                            degreef = (9*(degreec/5)+32);
                            System.out.println("The degree you entered in Fahrenheit" + 
                            degreec + "C");

            else
                    System.out.println("Error: incorrect letter usage.");

            }
    }
}

此代码正常运行,但第27行编译时声明:

时出错
"proj22.java:27: error: 'else' without 'if'
        else
        ^

1 error"

我该怎么做才能解决这个问题?

3 个答案:

答案 0 :(得分:5)

如果您有多行ifelsefor等,则需要添加括号,以便它们成为块并由编译器解释为单个语句:

else
{//you need to add a bracket here
    degreef = degree;
    degreef = (9*(degreec/5)+32);
    System.out.println("The degree you entered in Fahrenheit" + 
    degreec + "C");
}//and here

此外,最后一个之后的括号应该在它之前移动。

答案 1 :(得分:1)

您的if-else语句无效。你在用什么IDE?通常,如果代码中存在格式错误,IDE将不允许您运行程序,直到错误得到修复。

正确的if-else声明如下:

if (condition) {
   //statements
} else if (condition) {
   //statements
} else {
   //statements
}

答案 2 :(得分:1)

您的代码必须如下所示(检查代码中的注释以获得解释):

public static void main(String[] args)
{//Beginning of method  
    float degreef, degreec, degree;
    string cel = "c";
    string far = "f";
    string unit;
    System.out.println("Enter a temperture:");
    scanner keyboard = new Scanner (System.in);
    degree = keyboard.nextFloat();
    System.out.println("Enter a c to designate Celsius, or an f to designate..");        
    unit = keyboard.next();
    if (unit.equalsIgnoreCase(cel)||unit.equalsIgnoreCase(far))
    {//Beginning of first(1) if-statement
        if (unit.equalsIgnoreCase(cel))
        {//Beginning of second(2)/inner if-statement
            degreec = 5*(degreef-32)/9;
            System.out.println("The degree you entered in Celsius is" + 
            System.out.println("The degree you entered in Fahrenheit" + 
        }//End of second/inner if-statement
        else
        {//Begin-bracket for multi-line else        
            degreef = degree;
            degreef = (9*(degreec/5)+32);
            System.out.println("The degree you entered in Fahrenheit" + 
            degreec + "C");
        }//End-bracket for multi-line else  
    } //Needed to add this bracket to close first(1) if-statement
    else //*You could add a '{' here
        System.out.println("Error: incorrect letter usage.");
    //if you add a bracket at the beginning of else you'll have to close it here with '}'
    //removed bracket here
}//End of Method

如前所述,您需要用括号括起多行else语句。 此外,您需要在else语句之前关闭第一个if语句。