我正在使用一个程序,该程序会生成各种航空公司的阵列列表,以及每家航空公司飞行的收入里程和乘客里程数。这是我当前使用代码的位置:

/*
File: AirDataListTester.java

This file contains 3 classes:

  1. The AirData class.  Stores data for an airline - the name and
     the number of revenue miles and pasenger miles flown.
     (You will make no changes to this class)

  2. The AirDataList class. Maintains a list of AirData objects and has methods
     to add an object to the list and to return the list as a multi-line string
     (You will add a new method to this class but not modify the existing ones)

  3. The AirDataList class. Reads and echo prints lines from a data file
     until eof.  You will add statements in the indicated places to
     a. Create a Scanner object associated with the current line of input
     b. Call Scanner methods to extract the tokens from the line
     c. Create an AirData object using the tokens
     d. Add the object to the list
*/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


/*
 * A class to store data for an airline
 */
class AirData
{
    // instance vars

    private String name;        // airline name
    private int revenueMiles;   // annual revenue miles (in 1000's)
    private int passengerMiles; // annual passenger miles (in 1000's)

    /**
     * Creates an AirData object.
     *
     * @param name the airline name
     * @param revenueMiles the number of revenue miles flown
     * @param passengerMiles the number of passenger miles flown
     */
    public AirData(String name, int revenueMiles, int passengerMiles)
    {
        this.name = name;
        this.revenueMiles = revenueMiles;
        this.passengerMiles = passengerMiles;
    }

    /**
     * Returns the airline name.
     *
     * @return the airline name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the airline's revenue miles flown.
     *
     * @return the revenue miles
     */
    public int getRevMiles()
    {
        return revenueMiles;
    }

    /**
     * Returns the airline's passenger miles flown.
     *
     * @return the passenger miles
     */
    public int getPassMiles()
    {
        return passengerMiles;
    }
} // end of AirData class definition ========================================

/**
 * A class to implement a list of AirData objects
 */
class AirDataList
{
    // instance var
    private ArrayList<AirData> list;    // list of AirData objects

    /**
     * Creates an empty list
     */
    public AirDataList()
    {
        list = new ArrayList<AirData>();
    }

    /**
     * Appends an AirData object to the list.
     *
     * @param current the object to be appended to the list
     */
    public void addToList(AirData current)
    {
        list.add(current);  // calls add method of ArrayList class
    }


    public int totRevMiles()
    {
        int rev = 0;

        for (int i = 0; i < list.size(); i++)
        {
            AirData revMiles = list.get(i);
            rev = rev + revMiles.getRevMiles();
        }
        return rev;
    }

    public int totPassMiles()
    {
        int pass = 0;

        for (int i = 0; i < list.size(); i++)
        {
            AirData passMiles = list.get(i);
            pass = pass + passMiles.getPassMiles();
        }
        return pass;
    }


    /**
     * Converts the list to a multi-line string, with each line containing the
     * data for one airline.
     *
     * @return the String containing all the data on the list
     */
    public String toString()
    {
        // headings
        String out =
              String.format("%28s%18s%n", "Revenue Miles", "Passenger Miles") +
              String.format("%12s%16s%18s%n", "Airline", "(in 1000's) ",
                            "(in 1000's)  ") +
              String.format("%12s%16s%18s%n","=======","=============",
                            "===============");

        // for each AirData object on the list...
        for (int i = 0; i < list.size(); i++)
        {
            AirData air = list.get(i);             // get next AirData obj
            String name = air.getName();           // get airline name
            int revMiles = air.getRevMiles();      // get revenue miles
            int passMiles = air.getPassMiles();    // get passenger miles
            // concatenate data to output string
            out = out + String.format("%12s", name)
                    + String.format("%16s", revMiles)
                    + String.format("%18s", passMiles) + "\n";
        }
        return out + "\n";
    }

    public String anotherString()
    {
        String out = " Shares of Revenue Shares of Passengers\n "
        + " Airline (in %) (in %)\n"
        + " ======= ================= ====================" + "\n";

        for (int i = 0; i < list.size(); i++)
        {
            AirData air = list.get(i);
            String name = air.getName();
            int totRev = ((air.getRevMiles()/totRevMiles())*100);
            int totPop = ((air.getPassMiles()/totPassMiles())*100);


            out = out + String.format("%12s", name)
                    + String.format("%16s", totRev)
                    + String.format("%18s", totPop) + "\n";
        }
        return out + "\n";
    }

} // end of AirDataList class definition =====================================

public class AirDataListTester
{

    public static void main(String[] args) throws IOException
    {
        AirDataList list = new AirDataList();

        // create Scanner object to read each line of file until eof
        Scanner infile = new Scanner(new File("AirData.txt"));

        System.out.println("Data entered:\n");

        while (infile.hasNext()) // while not eof...
        {
            // read next line
            String line = infile.nextLine();

            // "echo print" data entered
            System.out.println(line);

            // a. create a Scanner object associated with String "line"

            Scanner filescan = new Scanner(line);



            // b. extract the 3 tokens from the current line

            while (filescan.hasNext())
            {

            String name = filescan.next();
            int revenueMiles = filescan.nextInt();
            int passengerMiles = filescan.nextInt();



            // c. create an AirData object passing the tokens to the constructor

            AirData next = new AirData(name,revenueMiles,passengerMiles);

            // d. add the object to the list

            list.addToList(next);

            }
        System.out.println();

        System.out.println(list.toString());  // print the list
        System.out.println(list.anotherString());
    }
} // end of AirDataListTester class definition


我正在尝试获取代码以正确打印每个航空公司在总收入里程以及总乘客里程中的份额。
以下是我尝试运行程序时得到的输出。

Data entered:

American 26851 2210871

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American             100               100


Continental 9316 622534

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871
 Continental            9316            622534


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American               0                 0
 Continental               0                 0


Delta 21515 1862276

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871
 Continental            9316            622534
       Delta           21515           1862276


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American               0                 0
 Continental               0                 0
       Delta               0                 0


Northwest 20803 1924288

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871
 Continental            9316            622534
       Delta           21515           1862276
   Northwest           20803           1924288


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American               0                 0
 Continental               0                 0
       Delta               0                 0
   Northwest               0                 0


USAir 9855 1542800

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871
 Continental            9316            622534
       Delta           21515           1862276
   Northwest           20803           1924288
       USAir            9855           1542800


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American               0                 0
 Continental               0                 0
       Delta               0                 0
   Northwest               0                 0
       USAir               0                 0


TransWorld 16228 1188124

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871
 Continental            9316            622534
       Delta           21515           1862276
   Northwest           20803           1924288
       USAir            9855           1542800
  TransWorld           16228           1188124


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American               0                 0
 Continental               0                 0
       Delta               0                 0
   Northwest               0                 0
       USAir               0                 0
  TransWorld               0                 0


United 35175 3673152

               Revenue Miles   Passenger Miles
     Airline    (in 1000's)      (in 1000's)
     =======   =============   ===============
    American           26851           2210871
 Continental            9316            622534
       Delta           21515           1862276
   Northwest           20803           1924288
       USAir            9855           1542800
  TransWorld           16228           1188124
      United           35175           3673152


 Shares of Revenue Shares of Passengers
  Airline (in %) (in %)
 ======= ================= ====================
    American               0                 0
 Continental               0                 0
       Delta               0                 0
   Northwest               0                 0
       USAir               0                 0
  TransWorld               0                 0
      United               0                 0


我将如何修复代码,以便它将正确打印航空公司份额,而不是仅打印所有零。 public string anotherString()方法是我尝试纠正的方法。

最佳答案

您正在使用整数除法,该整数将值舍入到较低的整数。例如。如果除以3 / 2,将得到1而不是1.5

您可以先将其转换为两倍:

int totRev = (int) ((((double)air.getRevMiles())/totRevMiles())*100);
int totPop = (int) ((((double)air.getPassMiles())/totPassMiles())*100);


或重新排序操作,以免出现四舍五入的情况:

int totRev = air.getRevMiles() * 100 / totRevMiles();
int totPop = air.getPassMiles() * 100 / totPassMiles();


这两个选项都应根据需要工作。

09-26 09:43