【问题标题】:.class expected and illegal expression.class 预期和非法表达式
【发布时间】:2015-05-18 20:39:15
【问题描述】:

你们中的任何人都可以帮助我完成我正在做的这个程序吗?我正在尝试编译它,但无法弄清楚它发生了什么。有两个 java 文件,我将只发送有问题的主要文件(直到现在)。这是一个计算公司每月利润的简单程序。

import java.util.Scanner;

public class MonthExe{
    public static void main( String[] args ){
        Scanner input = new Scanner( System.in );
        System.out.println( "Inform the current year..." );
        public int curYear = input.nextInt();
        public int catNum;
        String control = "n";
        while ( control = "n" ){
            System.out.println( "Inform the current month..." );
            int curMonth = input.nextInt();
            Month month = new Month( curMonth );
            month.monthMessage( curMonth );
            String control = input.next();
        }
        System.out.println( "Inform the number of arts..." );
        curArts = input.nextInt();
        System.out.println( "Inform the number of categories from the cheapest to the most expensive one..." );
        catNum = input.nextInt();
        curVals = new double[ catNum ];
        for ( int i = 0; int = catNum; i++ ){
            System.out.println( "Inform the price of the category" + ( i + 1 ) );
            curVals[i] = input.nextDouble();
        }
        Month month = new Month( curYear, curArts, curVals );
        for ( int j = 1; int = curArts; j++ ){
            for ( int k = 1; int = curVals.length; k++ ){
                System.out.println( "Inform the amount of units sold for product " + j + " and category " + k );
                int curAmount = input.nextInt();
                month.setProfit( curAmount, k );
                month.setTotalProfit( month.getTotalProfit() );
            }
        }
        month.finalMessage();
        System.out.println( "Thank you for using this software!" );
    }
}

尝试编译上述程序会返回这 5 个错误...有人可以解释它们的含义吗?提前谢谢!

MonthExe.java:7:错误:表达式的非法开始 public int curYear = input.nextInt();

MonthExe.java:8:错误:表达式的非法开始 public int catNum;

MonthExe.java:22:错误:预期为“.class” for ( int i = 0; int

MonthExe.java:27:错误:预期为“.class” for (int j = 1; int

MonthExe.java:28: 错误: '.class' 预期 for (int k = 1; int

【问题讨论】:

    标签: java


    【解决方案1】:

    您不能在方法中声明public 变量。删除关键字public

    【讨论】:

    • 谢谢!!我忘了!
    【解决方案2】:

    对于错误 #1 和 2,删除 public 关键字,您不能在方法内限定变量(更多 here

    对于错误 #3、4 和 5,for 循环声明中的第二个表达式需要计算为布尔值。对于#3 int = catNum 应该是这样的:i < catNum。 #4 和 5 相同。(有关 for 循环 here 的更多信息)

    【讨论】:

    • 我把它们三个都改成了
    • 您是否将int 更改为ijk
    猜你喜欢
    • 2015-08-06
    • 2018-10-15
    • 2021-11-16
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    相关资源
    最近更新 更多