错误表达式的非法开始

Posted

技术标签:

【中文标题】错误表达式的非法开始【英文标题】:error illegal start of expression 【发布时间】:2014-03-09 20:58:08 【问题描述】:

我不断得到:

表达式的非法开始,';" 预期 System.exit(0),并达到 解析时文件结束

这是它应该做什么和代码的描述。

要解决的问题: 我应该在课外学习多少?

问题: 你的同学需要帮助。这是他们上大学的第一年,他们需要确定需要学习多少小时才能获得好成绩。

Study Hrs Per Week Per Credit                 Grade
5:                                            A,
4:                                            B,
3:                                            C,
2:                                            D,
0:                                            F,

项目规格:

    用户可以输入他们计划每人学习的小时数 周或他们想要的等级基于他们的学分 进入。 (注意:您的程序必须为用户提供两个选项。) 程序显示用户名、学分数、总数 学习小时数,以及他们应该期望获得的成绩。 您可以使用介绍中介绍的任何编程技术 编程逻辑课程来完成这个项目。那将 但是,要求您在课本中提前阅读 关于如何在 Java 中实现该技术的说明 编程语言。

下面是我的代码:

import javax.swing.JOptionPane; //Needed for the dialogue box

/**
    This program shows the student the grade they will receive 
    or how much to study for a certain grade.
*/

public class MoultonAlainaProject1

    public static void main(String[] args)
    
        string wantedGrade, desiredGrade, name, input;
        char finalGrade;
        int gradeA = 5;
        int gradeB = 4;
        int gradeC = 3;
        int gradeD = 2;
        int gradeF = 0;
        float studyHrs;
        int creditHrs, userChoice;

        //Scanner for keyboard input
        Scanner keyboard = new Scanner(System.in);

        name = JOptionPane.showInputDialog("Please enter your name.");

        input = JOptionPane.showInputDialog("How many credit hours do you plan on taking this semester?");
        creditHrs = integer.parseInt(input);

        input = JOption.showInputDialog("If you would like to know the amount of hours you should study for the amount of" + 
                                        "credit hours press 1. If you would like to know the grade you would receive by" +
                                        "entering the amount of hours you plan to study press 2.");
        userChoice = integer.parseInt(input);

        if (userChoice == 1)
        
            desiredGrade = JOptionPane.showInputDialog("Enter the grade you would like to receive.");

            if (desiredGrade == 'A' || 'a')
            
                wantedGrade = gradeA;
            
            else
            
                if (desiredGrade == 'B' ||'b')
                
                    wantedGrade = gradeB;
                
                else
                
                    if (desiredGrade == 'C' || 'c')
                    
                        wantedGrade = gradeC;
                    
                    else
                    
                        if (desiredGrade == 'D' || 'd')
                        
                            wantedGrade = gradeD;
                        
                        else
                        
                            if (desiredGrade == 'F' || 'f')
                            
                                wantedGrade = gradeF;
                            
                            else 
                            
                                System.out.printIn("Error: you have not entered a valid input.");
                            
                        
                       
                
               

            studyHrs = wantedGrade * creditHrs; 
        
    else
    
        if (userChoice == 2)
        
            input = JOptionPane.showInputDialog("Enter the amount of hours you plan to study a week.");
            studyHrs = integer.parseInt(input);

            wantedGrade = studyHrs/creditHrs;

            if (wantedGrade >= 5)
            
                finalGrade = "A";
            
            else
            
                if (wantedGrade >= 4)
                
                    finalGrade = "B";
                
                else 
                
                    if (wantedGrade >= 3)
                    
                        finalGrade = "C";
                    
                    else
                    
                        if (wantedGrade >= 2)
                        
                            finalGrade = "D";
                        
                        else
                        
                            if (wantedGrade < 2)
                            
                                finalGrade = "F";
                            
                        
                    
                
            

        JOptionPane.showMessageDialog(null, name+ ", you are taking " +creditHrs+ "credit hours, you should study " +studyHrs+ " hours a week to receive " +finalGrade+ " in your classes." 

        System.exit(0);
    
   

我已经检查了很多次牙套,但我可能在某个地方漏掉了一些东西

【问题讨论】:

string wantedGrade, desiredGrade, name, input; 应该是 String wantedGrade, desiredGrade, name, input; (除非您创建了一个 string 类,这将是一个非常糟糕的名称) 字符串在 Java 中大写。 【参考方案1】:

您有大约 20 个编译时错误。 我看到缺少导入、用小写字母书写的字符串、未闭合的括号, 未闭合的花括号,将字符串值分配给 char 变量等。 请参见下文并修复它们。

    Description Resource    Path    Location    Type
    Type mismatch: cannot convert from String to char   MoultonAlainaProject1.java  /TEST/src   line 107    Java Problem
    Type mismatch: cannot convert from String to char   MoultonAlainaProject1.java  /TEST/src   line 101    Java Problem
    Type mismatch: cannot convert from String to char   MoultonAlainaProject1.java  /TEST/src   line 95 Java Problem
    Type mismatch: cannot convert from String to char   MoultonAlainaProject1.java  /TEST/src   line 89 Java Problem
    integer cannot be resolved  MoultonAlainaProject1.java  /TEST/src   line 83 Java Problem
    The method printIn(String) is undefined for the type PrintStream    MoultonAlainaProject1.java  /TEST/src   line 69 Java Problem
    integer cannot be resolved  MoultonAlainaProject1.java  /TEST/src   line 33 Java Problem
    JOption cannot be resolved  MoultonAlainaProject1.java  /TEST/src   line 30 Java Problem
    Syntax error, insert "" to complete MethodBody MoultonAlainaProject1.java  /TEST/src   line 124    Java Problem
    Syntax error on token(s), misplaced construct(s)    MoultonAlainaProject1.java  /TEST/src   line 122    Java Problem
    exit cannot be resolved or is not a field   MoultonAlainaProject1.java  /TEST/src   line 122    Java Problem
    Syntax error on token(s), misplaced construct(s)    MoultonAlainaProject1.java  /TEST/src   line 120    Java Problem
    Type mismatch: cannot convert from String to char   MoultonAlainaProject1.java  /TEST/src   line 113    Java Problem
    Syntax error, insert "" to complete ClassBody  MoultonAlainaProject1.java  /TEST/src   line 9  Java Problem
    Scanner cannot be resolved to a type    MoultonAlainaProject1.java  /TEST/src   line 23 Java Problem
    string cannot be resolved to a type MoultonAlainaProject1.java  /TEST/src   line 12 Java Problem
    integer cannot be resolved  MoultonAlainaProject1.java  /TEST/src   line 28 Java Problem
    Scanner cannot be resolved to a type    MoultonAlainaProject1.java  /TEST/src   line 23 Java Problem

【讨论】:

【参考方案2】:
 JOptionPane.showMessageDialog(null, name+ ", you are taking " +creditHrs+ "credit hours, you should study " +studyHrs+ " hours a week to receive " +finalGrade+ " in your classes." 

showMessageDialog() 方法不完整。您缺少右括号和分号。

【讨论】:

以上是关于错误表达式的非法开始的主要内容,如果未能解决你的问题,请参考以下文章

非法的表达式开始

java中非法的表达式开始

非法的表达式的开始

与私有变量相关的非法表达式开头

java非法的表达式开始

错误:某些 lambda 表达式中的表达式开头非法