0

在我的代码中,我必须计算航班延误的航班数量/航班总数。我已经编写了代码,除了最终的比率打印输出外,一切正常。有人看到问题了吗?

import au.com.bytecode.opencsv.CSVReader;
// javac -classpath .;opencsv-2.3.jar flightinfo.java
import java.util.*;
import java.io.*;
import java.lang.*;


public class flightinfo2{

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

    CSVReader reader = new CSVReader(new FileReader("delayed.csv"));
    String [] nextLine;

    Map<String, Carrier> airlines = new HashMap<String, Carrier>();         

    while ((nextLine = reader.readNext()) != null) {
            // At this point, nextLine is the name of the carrier, e.g., "Continental"
            // nextLine can be used as the key in your "airlines" map. E.g., airlines.put(nextLine, new Carrier());

        if (airlines.containsKey(nextLine[1])){
        airlines.get(nextLine[1]).addNumFlights(Integer.valueOf(nextLine[6]));
        airlines.get(nextLine[1]).addNumDelayed(Integer.valueOf(nextLine[7]));
        airlines.get(nextLine[1]).setDelayedRatio();
        }
        else {
          airlines.put(nextLine[1], new Carrier());
          airlines.get(nextLine[1]).setName(nextLine[1]);
          airlines.get(nextLine[1]).setNumFlights(Integer.valueOf(nextLine[6]));
          airlines.get(nextLine[1]).setNumDelayed(Integer.valueOf(nextLine[7]));
        }
    }
airlines.get("Continental").print();
airlines.get("Atlantic Southeast").print();
airlines.get("American Eagle").print();
airlines.get("Delta").print();
airlines.get("ExpressJet").print();
airlines.get("United").print();
airlines.get("SkyWest").print();
airlines.get("US Airways").print();
airlines.get("JetBlue").print();
airlines.get("American").print();
airlines.get("Mesa").print();
airlines.get("Frontier").print();
airlines.get("AirTran").print();
airlines.get("Southwest").print();  

}
} 

班上

public class Carrier{
String name;
int totalFlights;
int delayedFlights;
int totalDelayedRatio;

public Carrier(){}

public void setName(String s){
name = s;
}
public void setNumDelayed(int n){
delayedFlights = n;
}

public void setNumFlights(int s){
totalFlights = s;
}

public void addNumDelayed(int n){
delayedFlights = delayedFlights + n;
}

public void addNumFlights(int s){
totalFlights = delayedFlights + s;
}

public void setDelayedRatio(){
totalDelayedRatio = (delayedFlights/totalFlights);
}

public void print(){
System.out.printf("%s:\t%d,\t%d,\t%d\n", name, totalFlights, delayedFlights, totalDelayedRatio);
}

}

打印输出如下: Continental: 92, 81, 0 <---应该是比率...更多运营商

4

1 回答 1

0

代替:

totalDelayedRatio = (delayedFlights/totalFlights);

尝试明确使用“this”:

this.totalDelatedRatio = (delayedFlights/totalFlights);
于 2012-09-27T02:08:36.403 回答