Java程序未编译

时间:2018-10-09 19:31:24

标签: java

我根据某人输入他们的考试成绩进行了分配,并根据他们的分数进行了一堆if else陈述,并进行了四舍五入。不幸的是它无法编译,我也不知道为什么。

非常感谢您的帮助

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner keyboard = new Scanner(System.in);
    int selection;
    double listeningScore, speakingScore, readingScore, writingScore, overallScore1;

    System.out.println("-------****-------****-------****-------****-----****----- ");
    System.out.println("Welcome to Concordia Language Proficiency Evaluator! \n\t based on IELTS exam");
    System.out.println("-------****-------****-------****-------****-----****----- ");

    System.out.println("Here are the available options:");
    System.out.println("\t1 - Language Proficiency Requirements for the Applicant");
    System.out.println("\t2 - Evaluation of your language proficiency");

    System.out.print("\nPlease enter the digit corresponding to your case: ");
    selection = keyboard.nextInt();

    if (selection == 1)
    {
        System.out.println("\n- The overall score of IELTS exam of applicants needs to be equal or above 6.5 and the scores" 
                + "\nfor writing and reading skills should not be below 6.0. If your overall score is 6, and your reading and writing scores are at least 6, you will be eligible for conditional admission."
                + "\nSo you need to take an English course in the first semester. Otherwise you have to retake the IELTS exam." 
                + "\nThanks for choosing Concordia. ");
    }

    else if (selection ==2)

        {
        System.out.print("\nPlease enter the your listening score: ");
        listeningScore = keyboard.nextDouble();

        System.out.print("\nPlease enter the your speaking score: ");
        speakingScore = keyboard.nextDouble();

        System.out.print("\nPlease enter the your reading score: ");
        readingScore = keyboard.nextDouble();

        System.out.print("\nPlease enter the your writing score: ");
        writingScore = keyboard.nextDouble();

}

    double overallScore1 = (listeningScore + speakingScore + readingScore + writingScore) /4 ;

        if (overallScore1 - (int)overallScore1 <0.25)
            overallScore1 = (int)overallScore1;

            else if (overallScore1 - (int)overallScore1 >=0.25 && overallScore1 - (int) overallScore1 <0.75)
                overallScore1 = (int)overallScore1 + 0.5;

                else if (overallScore1 - (int)overallScore1 >= 0.75)
                    overallScore1 = (int)++overallScore1;


        System.out.print("\n\t Your overall score is: " + overallScore1);
        System.out.print("\n\tYour reading score is: " + readingScore);
        System.out.print("\n\tYour writing score is: " + writingScore);


            else if (overallScore1 >= 6.5 && readingScore >=6 && writingScore >=6)
                    System.out.print("\t\n Congratulations: You are eligible for admission");

            else if (overallScore1 >=6.5 && readingScore <6 || writingScore <6)
                System.out.print("\t\n Congratulations: You are eligible for a conditional admission. You must take an English course in the first semester.");

            else if (overallScore1 == 6 && readingScore >=6 && writingScore >=6)
                System.out.print("\t\n Congratulations: You are eligible for a conditional admission. You must take an English course in the first semester.");

            else if (overallScore1 == 6 && readingScore <6 || writingScore <6)
                System.out.print("\t\n Unfortunately, you must retake the exam.");

            else if (overallScore1 <6)
                System.out.print("\t\n Unfortunately, you must retake the exam.");


}

}

编译错误是

  

线程“ main”中的异常java.lang.Error:未解决的编译问题:

     

重复的局部变量totalScore1

     

令牌“ else”上的语法错误,请在Comp248 / Assignment.assignment2.main(assignment2.java:50)上删除此令牌

1 个答案:

答案 0 :(得分:1)

在第四行中,您已经声明了“ overallscore1”,然后在if和else if之后,再次声明了它。不允许在同一块中两次声明变量。所以在这里,如果您想使用先前的变量,则需要编写

overallscore1= = (listeningScore + speakingScore + readingScore + writingScore) 

我认为您现在将再遇到一个错误,“否则就没有错误”。试试这个

 else if (overallScore1 - (int)overallScore1 >= 0.75)
 {         
    overallScore1 = (int)++overallScore1;
    System.out.print("\n\t Your overall score is: " + overallScore1);
    System.out.print("\n\tYour reading score is: " + readingScore);
    System.out.print("\n\tYour writing score is: " + writingScore);
 }
 else if (overallScore1 >= 6.5 && readingScore >=6 && writingScore >=6)
    System.out.print("\t\n Congratulations: You are eligible for admission");

如果您要为特定情况编写多个语句,请使用“ {}”。 例如。

if(condition1)
{
  Statement 1
  Statement 2
  Statement n
}
else
{
   Statement 1
   Statement 2
   Statement n
 }