Java 类型和“”的非法开始&引用;预期

Java 类型和“”的非法开始&引用;预期,java,if-statement,syntax-error,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

我试图在这里写一个程序,我开始时有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!");




    }
}
/**
*@(#)Exercise1Lab5.java
*
*
*@作者
*@version 1.00 2014/11/3
*/
导入java.util.Scanner;
公共课练习1LAB5{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
漂浮年龄、身高、体重;
字符串genderAns、recordAns、certAns、courseAns;
字符y,n,m,f;
System.out.print(“\n请输入您的年龄:”);
age=input.nextFloat();
如果(年龄>=18岁和年龄=1.6岁){
System.out.print(“\n请输入您的体重,单位为KG:”;
权重=输入。nextFloat();
如果(重量>=100){
System.out.print(“\n请输入您的性别:”;
genderAns=input.nextLine();
如果(((genderAns.equals('m'))和&(weight=1.85))| |((genderAns.equals('f'))和&(weight=1.6)){
System.out.print(“\n您是否有犯罪记录?:”);
recordAns=input.nextLine();
if(recordAns.equals('n'))
System.out.print(“\n您是否在毕业证书中获得至少D级的爱尔兰语及格?:”;
certAns=input.nextLine();
System.out.print(“\n您是否承诺参加为期10周的爱尔兰申请课程?:”;
courseAns=input.nextLine();
}
其他的
System.out.println(“\n您不能有犯罪记录!”);
}
其他的
System.out.println(“\n到目前为止,您不符合要求!”);
}
其他的
System.out.println(“\n您的体重必须低于100公斤!”);
}
其他的
System.out.println(“\n您不够高!”);
}
其他的
System.out.println(“\n您要么太老,要么太年轻!”);
}
}

如果有人能解释这些错误并帮我解决,我将非常感激,谢谢

您的括号不匹配。在这里:

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();

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

代码需要如下更改:

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();

}
if(recordAns.equals('n')){
System.out.print(“\n您是否在毕业证书中获得至少D级的爱尔兰语及格?:”;
certAns=input.nextLine();
System.out.print(“\n您是否承诺参加为期10周的爱尔兰申请课程?:”;
courseAns=input.nextLine();
}

在最里面的
if
后面缺少一个
{
。这是“即使块中只有一条语句,也要始终使用大括号”的另一个很好的论点。了解DeMorgan的规则,并将代码重写为
if(failureCondition){failure message;return;}
我喜欢尽量减少返回点的数量,这只是出于个人偏好,但只要清楚发生了什么,早期返回是可以的。我不喜欢太多返回点,但它们比如果他不得不重组的话将会发生的hijinks更好在这种情况下,他可以用
float value=getFloatFailingIfNotInRange(float-low、float-high、String-prompt、String-failMessage)替换大部分逻辑
谢谢你的回答和提示!我在放入丢失的花括号后成功地运行了它,而且还有一个括号丢失了,所以我也修复了它。但是现在当我运行程序时,发生了以下情况:“请输入您的年龄:18请输入您的身高:1.9请输入您的体重单位‘KG’:60请输入您的性别?:到目前为止您还不符合要求!”只是跳过输入“m或f”“option….@alannm37如果您已经解决了此帖子中的问题,并且遇到了新问题,请发布一个新问题;但请确保您的问题陈述清楚,并确保向我们展示您的调试尝试!我在90分钟内无法发布另一个问题,但到时候我会发布。
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();

}