运行错误:找不到符号

时间:2014-04-02 15:46:54

标签: java class symbols

该错误低于以下内容:

  

symbol:class CarbonDioxideData location:class CarbonAnalysis     public static void category(CarbonDioxideData []数据,int size,   扫描仪输入,PrintStream输出){                                 ^

     

CarbonAnalysis.java:65:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   getData(Scanner c,CarbonDioxideData [] data,int size){                                           ^

     

CarbonAnalysis.java:81:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   swap(CarbonDioxideData [] data,int pose1,int pose2){                             ^

     

CarbonAnalysis.java:88:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   total(CarbonDioxideData []数据,int size,PrintStream输出){                              ^

     

CarbonAnalysis.java:107:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   co2per(CarbonDioxideData []数据,int size,PrintStream输出){                               ^

     

CarbonAnalysis.java:125:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   road(CarbonDioxideData []数据,int size,PrintStream输出){                             ^

     

CarbonAnalysis.java:143:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   cars(CarbonDioxideData []数据,int size,PrintStream输出){                             ^

     

CarbonAnalysis.java:161:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   findCanada(CarbonDioxideData []数据,int size,String name,   PrintStream输出){                                   ^

     

CarbonAnalysis.java:173:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis public static void   printList(CarbonDioxideData []数据,int size,String s,PrintStream   输出){                                  ^

     

CarbonAnalysis.java:32:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis CarbonDioxideData []   data = new CarbonDioxideData [size]; ^

     

CarbonAnalysis.java:32:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis CarbonDioxideData []   data = new CarbonDioxideData [size];                                        ^

     

CarbonAnalysis.java:67:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis数据[i] =新   CarbonDioxideData();                           ^

     

CarbonAnalysis.java:82:找不到符号符号:class   CarbonDioxideData位置:类CarbonAnalysis CarbonDioxideData   温度=数据[pose1]; ^

以下是代码:

public static void main(String[] args) throws FileNotFoundException{

    Scanner input = new Scanner(System.in);

    PrintStream output = new PrintStream(new File("carbon_report.txt"));

    if(args.length == 0){
        System.out.println("No file command line found.");
        System.out.println("Please type a filename");
        System.exit(0);
    }
    // get a valid filename
    File f = new File(args[0]);
    while(f.exists() == false){
        System.out.println("File doesn't exist. Please type a new name.");
        String filename = input.nextLine();
        f = new File(filename);
        }

        Scanner console = new Scanner(f);
        console.nextLine();
        int size = console.nextInt();


    // create a new array of CarbonDioxideData
    CarbonDioxideData[] data = new CarbonDioxideData[size]; 
    getData(console, data, size);
    category(data, size, input, output);

}
// get which categories the user want to analyze
public static void category(CarbonDioxideData[] data, int size, Scanner input, PrintStream output){
    System.out.printf("%20s%30s", "1. totalCO2emissions", "2. roadCO2emissions");
    System.out.printf("%50s%25s", "\n3. per-personRoadEmissions", "4. carsPer1000People");
    System.out.println("\nHow many categories do you choose to analyze?");
    int num = input.nextInt();

    if(num>0){
        System.out.println("Pleas choose the number of category you want to analyze(the number is in the front of each category):");
        for(int i=1; i<=num; i++){
            int category = input.nextInt();
            if(category==1){
                total(data,size, output);
                output.println();
            }else if(category==2){
                road(data, size, output);
                output.println();
            }else if(category==3){
                co2per(data, size, output);
                output.println();
            }else if(category==4){
                cars(data, size, output);
                output.println();
            }
        }
    }
}
// set the array of CarbonDioxideData
public static void getData(Scanner c, CarbonDioxideData[] data, int size){
    for(int i=0; i<size; i++){
        data[i] = new CarbonDioxideData();
        String country = c.next();
        data[i].setCountry(country);
        double total = c.nextDouble();
        data[i].setTotalCO2(total);
        double road = c.nextDouble();
        data[i].setRoadCO2(road);
        double co2per = c.nextDouble();
        data[i].setCO2PerPerson(co2per);
        int cars = c.nextInt(); 
        data[i].setCarsPerPerson(cars);
    }
}

public static void swap(CarbonDioxideData[] data, int pose1, int pose2){
    CarbonDioxideData temp=data[pose1];
    data[pose1]=data[pose2];
    data[pose2]=temp; 
}

// compare total CO2 emissions 
public static void total(CarbonDioxideData[] data, int size, PrintStream output){
    for(int i=0; i<size-1; i++){
        int min = i;
        for(int j=i+1; j<size; j++){
            if(data[j].getTotalCO2()<data[min].getTotalCO2()){
                min = j;
            }

        }
        swap(data,i,min);
    }
    printList(data, size, "total emissions", output);
    output.println("Country with the lowest total emissions: " + data[0].getCountry());
    output.println("Country with the highest total emissions: " + data[size-1].getCountry());
    findCanada(data, size, "lowest total emissions", output);

}

// compare the amount of per-person road emission
public static void co2per(CarbonDioxideData[] data, int size, PrintStream output){
    for(int i=0; i<size-1; i++){
        int min = i;
        for(int j=i+1; j<size; j++){
            if(data[j].getCO2PerPerson()<data[min].getCO2PerPerson()){
                min = j;
            }
        }
        swap(data,i,min);
    }
    printList(data, size, "per-person road emissions",output);
    output.println("Country with the lowest per-person road emissions: " + data[0].getCountry());
    output.println("Country with the highest per-person road emissions: " + data[size-1].getCountry());
    findCanada(data, size, "lowest per-person road emissions.", output);

}

// analyze the road emissions
public static void road(CarbonDioxideData[] data, int size, PrintStream output){
    for(int i=0; i<size-1; i++){
        int min = i;
        for(int j=i+1; j<size; j++){
            if(data[j].getRoadCO2()<data[min].getRoadCO2()){
                min = j;
            }
        }
        swap(data,i,min);
    }
    printList(data, size, "road CO2 emissions",output);
    output.println("Country with the lowest road emissions: " + data[0].getCountry());
    output.println("Country with the highest road emissions: " + data[size-1].getCountry());
    findCanada(data, size, "lowest road emissions.", output);

}

// analyze the number of cars per 1000 people
public static void cars(CarbonDioxideData[] data, int size, PrintStream output){
    for(int i=0; i<size-1; i++){
        int min = i;
        for(int j=i+1; j<size; j++){
            if(data[j].getCarsPerPerson()<data[min].getCarsPerPerson()){
                min = j;
            }
        }
        swap(data,i,min);
    }
    printList(data, size, "cars per 1000 people",output);
    output.println("Country with the lowest cars per 1000 people: " + data[0].getCountry());
    output.println("Country with the highest cars per 1000 people: " + data[size-1].getCountry());
    findCanada(data, size, "lowest cars per 1000 people.", output);

}

// find the position of Canada in the given category 
public static void findCanada(CarbonDioxideData[] data, int size, String name, PrintStream output){
    int n = 1;
    for(int i=0; i<size; i++){
        String country = data[i].getCountry();
        if(country.equals("Canada")){
            n=i;
        }
    }
    output.println("Canada is ranked " + (n+1) + " out of " + size+" for " +name);
}

// print the lists of total emissions or per-person road emission
public static void printList(CarbonDioxideData[] data, int size, String s, PrintStream output){
    if(s.equals("per-person road emissions")){
        output.println("The precedence table of total emissions:");
        for(int i = 0; i<size; i++){    
            output.printf("%10s", data[i].getCountry());
            output.printf("%10.2f",data[i].getCO2PerPerson());
            output.println();
        }
    }else if(s.equals("total emissions")){
        output.println("The precedence table of "+ s);
        for(int i = 0; i<size; i++){
            output.printf("%10s ", data[i].getCountry());
            output.printf("%10.2f",data[i].getTotalCO2());
            output.println();
        }
    }else if(s.equals("road CO2 emissions")){
        output.println("The precedence table of "+ s);
        for(int i = 0; i<size; i++){
            output.printf("%10s ", data[i].getCountry());
            output.printf("%10.2f",data[i].getRoadCO2());
            output.println();
        }
    }else if(s.equals("cars per 1000 people")){
        output.println("The precedence table of "+ s);
        for(int i = 0; i<size; i++){
            output.printf("%10s ", data[i].getCountry());
            output.printf("%10d",data[i].getCarsPerPerson());
            output.println();
        }
    }
}

}

0 个答案:

没有答案