Java不接受模块名称和其他变量

时间:2017-11-22 20:14:05

标签: java module

今天我来寻求帮助,因为我的代码是我想要的方式,但是,我收到很多错误,所以我开始相信我不能使用我的模块名称和其他类似的术语。我很迷茫,我已经搜遍过,但没有什么可以解释我的模块名称是怎么来的"非法"。我已将它们复制并贴在这里,以防您觉得有用。我很困惑,因为你可以看到我的大多数错误"来自我的大部分模块的开始,以及开始。

错误:

    Lab8_5.java:12: error: illegal start of expression
                public static int []notGreenCost = new int[6];
                ^
Lab8_5.java:17: error: invalid method declaration; return type required
                getNotGreen();
                ^
Lab8_5.java:18: error: invalid method declaration; return type required
                getGoneGreen();
                ^
Lab8_5.java:19: error: invalid method declaration; return type required
                energySaved();
                ^
Lab8_5.java:20: error: invalid method declaration; return type required
                displayInfo();
                ^
Lab8_5.java:22: error: illegal start of type
                while(endProgram.equals("no")) {
                ^
Lab8_5.java:22: error: <identifier> expected
                while(endProgram.equals("no")) {
                                       ^
Lab8_5.java:22: error: illegal start of type
                while(endProgram.equals("no")) {
                                        ^rrors

代码:

import java.util.Scanner;
public class Lab8_5 {
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        // Declare variables
        public static int []notGreenCost = new int[6];
        public static int []goneGreenCost = new int[6];
        public static String []months = {"January","Februarry","March","April","May","June"};

        // Module calls
        getNotGreen();
        getGoneGreen();
        energySaved();
        displayInfo();

        while(endProgram.equals("no")) {
            System.out.println("Do you want to run again: yes or no");
            endProgram = keyboard.nextLine();
            while (!(endProgram.equals("yes")) && !(endProgram.equals("no"))){
                System.out.println("Please enter a value of yes or no");
                endProgram = keyboard.nextLine();
        }
    }
    // getNotGreen module
    static void getNotGreen(){
    Scanner in = new Scanner(System.in);
    for(int ctr=0;ctr<6;ctr++){
          System.out.println("Enter NOT GREEN energy costs for: "+months[ctr]);
          notGreenCost[ctr]= keyboard.nextLine();
        }
    }
    // getGoneGreen module
    static void getGoneGreen(){
    Scanner in = new Scanner(System.in);
    for(int ctr=0;ctr<6;ctr++){
          System.out.println("Enter GONE GREEN energy costs for: "+months[ctr]);
          goneGreenCost[ctr]= keyboard.nextLine();
        }
    }
    // energySaved module
    static void energySaved(){
    for(int ctr=0;ctr<6;ctr++){

        }
    }
    // diplayInfo module
    static void displayInfo(){
    System.out.println("Savings       "+"Not Green       "+"Gone Green   "+"Months");
    for(int ctr=0;ctr<6;ctr++){
        System.out.println((notGreenCost[ctr]-goneGreenCost[ctr])+"           "+notGreenCost[ctr]+"             "+goneGreenCost[ctr]+"          "+months[ctr]);
        }
    }    
}

1 个答案:

答案 0 :(得分:0)

在主要方法之后,您的代码中还有一个额外的“}”。

}   =======>EXTRA 
    // getNotGreen module
    static void getNotGreen(){
    Scanner in = new Scanner(System.in);
    for(int ctr=0;ctr<6;ctr++){
          System.out.println("Enter NOT GREEN energy costs for: "+months[ctr]);
          notGreenCost[ctr]=in.nextInt();
        }
    }

局部变量(方法中的变量)也不会使用任何访问修饰符,如“public”“private” 改变以下

 public static int []notGreenCost = new int[6];
        public static int []goneGreenCost = new int[6];
        public static String []months = {"January","Februarry","March","April","May","June"};

 int []notGreenCost = new int[6];
  int []goneGreenCost = new int[6];
  String []months = {"January","Februarry","March","April","May","June"};

我建议您阅读更多内容,只有当您理解了理论和语法后,才能输入并验证程序。 :)