如何纠正Java中的“非法启动表达”和其他错误?

时间:2015-09-27 10:58:44

标签: java if-statement

我不知道如何解决这些错误。这是我的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Ld1151rdb331 {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
        float x=0, y=0;
        String s;

        System.out.println("Paula Plauča IRDBD01 151RDB331");

        try {
            System.out.print("x=");
            s = br.readLine();
            x = Float.parseFloat(s);
            System.out.print("y=");
            s = br.readLine();
            y = Float.parseFloat(s);
        } catch(Exception e){
            System.out.println("input-output error");
            return;
        }

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
            System.out.println("green");
          else
            if (y>=5 && x>=2 && y<=9-x)
                     System.out.println("blue");
            else
                     if ((x>=2 && x<=5 || x>=2 && x<=10) && y>=3 && y<=7)
                    System.out.println("red");
                else
                    System.out.println("white");
    }
}

并显示以下错误:

compilation info

Main.java:25: error: illegal start of expression

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                        ^

Main.java:25: error: ';' expected

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                     ^
Main.java:25: error: not a statement

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))

                                        ^
Main.java:25: error: ';' expected

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))

                                          ^
Main.java:25: error: not a statement

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                       ^
Main.java:25: error: ';' expected

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                           ^
Main.java:25: error: not a statement

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))

                                                                         ^
Main.java:25: error: ';' expected

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                                   ^
Main.java:25: error: not a statement

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                                       ^

Main.java:25: error: ';' expected

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                                         ^
Main.java:25: error: not a statement

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                                             ^
Main.java:25: error: ';' expected

        if ((x-4)*(x-4)+(y-3)*(y-3)<=1) || (x-8)*(x-8)+(y-3)*(y-3)<=1) || (y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))
                                                                                                               ^

Main.java:27: error: 'else' without 'if'

          else
          ^
13 errors

也许有人可以提供帮助,我不明白。

2 个答案:

答案 0 :(得分:1)

if声明中的括号数量错误。解决此问题的一种方法是确保用括号括起每个逻辑条件:

if (((x-4)*(x-4)+(y-3)*(y-3)<=1) || 
    ((x-8)*(x-8)+(y-3)*(y-3)<=1) || 
    ((y>=5) && ((x-7.5)*(x-7.5)+(y-5)*(y-5)<=(2.5*2.5))))

答案 1 :(得分:1)

您的if语句缺少打开和/或右括号或格式不正确。这可以为您提供一个良好的开端,我建议您使用IDE以避免这些类型的错误。

 import java.io.BufferedReader;
 import java.io.InputStreamReader;

 public class Ld1151rdb331 {
 public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    float x = 0, y = 0;
    String s;

    System.out.println("Paula Plau?a IRDBD01 151RDB331");

    try {
        System.out.print("x=");
        s = br.readLine();
        x = Float.parseFloat(s);
        System.out.print("y=");
        s = br.readLine();
        y = Float.parseFloat(s);
    } catch (Exception e) {
        System.out.println("input-output error");
        return;
    }

    if (((x - 4) * (x - 4) + (y - 3) * (y - 3) <= 1)
            || ((x - 8) * (x - 8) + (y - 3) * (y - 3) <= 1)
            || ((y >= 5) && ((x - 7.5) * (x - 7.5) + (y - 5) * (y - 5) <= (2.5 * 2.5))))
        System.out.println("green");
    else if (y >= 5 && x >= 2 && y <= 9 - x)
        System.out.println("blue");
    else if ((x >= 2 && x <= 5 || x >= 2 && x <= 10) && y >= 3 && y <= 7)
        System.out.println("red");
    else
        System.out.println("white");
  }
}

HAPPY_CODING