非法的类型开始和&#34 ;;"预期

时间:2014-11-12 21:12:31

标签: java if-statement syntax-error

我正在尝试在这里编写一个程序,我首先启动了23个错误,我设法将其归结为7个错误,但根本不知道如何摆脱它们。错误消息如下:

Exercise1Lab5.java:58: error: illegal start of type
        else
        ^
Exercise1Lab5.java:58: error: ';' expected
        else
            ^
Exercise1Lab5.java:59: error: illegal start of type
                System.out.println("\nYou Are Either Too Old or Too Young!");
                      ^
Exercise1Lab5.java:59: error: ';' expected
                System.out.println("\nYou Are Either Too Old or Too Young!");
                          ^
Exercise1Lab5.java:59: error: invalid method declaration; return type required
                System.out.println("\nYou Are Either Too Old or Too Young!");
                           ^
Exercise1Lab5.java:59: error: illegal start of type
                System.out.println("\nYou Are Either Too Old or Too Young!");
                                   ^
Exercise1Lab5.java:65: error: class, interface, or enum expected
}
^
7 errors

我的代码是:

/**
 * @(#)Exercise1Lab5.java
 *
 *
 * @author 
 * @version 1.00 2014/11/3
 */

import java.util.Scanner;
public class Exercise1Lab5 {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    float age, height, weight;
    String genderAns, recordAns, certAns, courseAns;
    char y, n, m, f;

    System.out.print("\nPlease Enter Your Age: ");
    age = input.nextFloat();

    if(age>=18 && age<35){
        System.out.print("\nPlease Enter Your Height: ");
        height = input.nextFloat();

        if(height>=1.6){
            System.out.print("\nPlease Enter Your Weight In 'KG': ");
            weight = input.nextFloat();

            if(weight>=100){    
                System.out.print("\nPlease Enter Your Gender <m or f>?: ");
                genderAns = input.nextLine();

                if(((genderAns.equals('m')) && (weight<100) && (height>=1.85)) || ((genderAns.equals('f')) && (weight<100) && (height>=1.6))){ 
                    System.out.print("\nDo You Have A Criminal Record <y or n>?: ");
                    recordAns = input.nextLine();

                    if(recordAns.equals('n'))
                        System.out.print("\nDid You Receive At Least A D Grade In Pass Irish In The Leaving Cert <y or n>?: ");
                        certAns = input.nextLine();

                        System.out.print("\nDo You Commit To Taking A 10-Week Irish Course on Application <y or n>?: ");
                        courseAns = input.nextLine();
                    }
                    else
                        System.out.println("\nYou Cannot Have A Criminal Record!");
                }
                else
                System.out.println("\nThus Far You Do Not Meet The Requirements!"); 
            }
            else
                System.out.println("\nYou Need To Be Under 100KG In Weight!");
        }
        else
            System.out.println("\nYou Are Not Tall Enough!");
    }
    else
        System.out.println("\nYou Are Either Too Old or Too Young!");




    }
}

如果有人能够解释这些错误并帮助我,我会非常感激,谢谢!!

2 个答案:

答案 0 :(得分:1)

您的括号不匹配。这里:

if(recordAns.equals('n'))
    System.out.print("\nDid You Receive At Least A D Grade In Pass Irish In The Leaving Cert <y or n>?: ");
    certAns = input.nextLine();

{后需要if

有时这些错误可能令人困惑。一些提示:

  1. 从报告错误的行开始,然后小心地向后工作,检查是否存在细微问题。
  2. 使用可以显示匹配括号的合适编辑器。 Notepad++是基本编辑的不错选择。
  3. 如果您的编辑器支持自动缩进,请将其打开。你可以通过发现缩进问题来​​捕捉这样的事情。
  4. 如果可能的话,您有时也可以考虑重构代码,以澄清一点。例如,如果您希望坚持您输入的年龄在范围内:

    do {
        System.out.print("\nPlease Enter Your Age: ");
        age = input.nextFloat();
    } while (age < 18 || age >= 35);
    

    在该代码块之后,它保证age >= 18 && age < 35

答案 1 :(得分:0)

代码需要像这样改变:

if(recordAns.equals('n')) {
                        System.out.print("\nDid You Receive At Least A D Grade In Pass Irish In The Leaving Cert <y or n>?: ");
                        certAns = input.nextLine();

                        System.out.print("\nDo You Commit To Taking A 10-Week Irish Course on Application <y or n>?: ");
                        courseAns = input.nextLine();

}