-1
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"

我能做些什么来解决这个问题?

4

3 回答 3

5

当你有一个 multiline if, else,for等时,你需要添加括号,以便它们成为一个块并被编译器解释为单个语句:

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

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

于 2013-10-12T18:34:00.903 回答
1

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

正确的if-else说法如下:

if (condition) {
   //statements
} else if (condition) {
   //statements
} else {
   //statements
}
于 2013-10-12T18:39:06.413 回答
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 语句。

于 2013-10-12T18:39:12.363 回答