我正在使用一个程序来生成各种航空公司的阵列列表,以及每个航空公司飞行的收入里程数和乘客里程数。这是我目前使用代码的地方:
/*
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()
方法是我试图纠正的方法。
答案 0 :(得分:1)
您正在使用整数除法,它将值舍入到较低的值。例如。如果您划分public static void deleteFile() throws IOException {
JFileChooser fileDialog = new JFileChooser("C:\\ProgramData\\L1 Art Files\\");
File[] selectedFiles;
fileDialog.setSelectedFiles(null);
// Set frame properties
fileDialog.setDialogTitle("Delete Pixel Art File(s)");
//fileDialog.setLayout(new FlowLayout());
fileDialog.setSize(400, 400);
fileDialog.setVisible(true);
fileDialog.setMultiSelectionEnabled(true); // Allow multiple selection
fileDialog.setVisible(true);
int option = fileDialog.showDialog(null, "Delete");
if (option != JFileChooser.APPROVE_OPTION)
return; //user canceled the
selectedFiles = fileDialog.getSelectedFiles();
if (selectedFiles != null) { //ask the user to replace this file
int response = JOptionPane.showConfirmDialog(null, "Are you sure want to delete this file?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION) return;
}
for (File f : selectedFiles) {
Files.delete(f.toPath());
}
,则会获得3 / 2
而不是1
。
你可以先把它加倍:
1.5
或重新排序操作,这样就不会遇到舍入:
int totRev = (int) ((((double)air.getRevMiles())/totRevMiles())*100);
int totPop = (int) ((((double)air.getPassMiles())/totPassMiles())*100);
这两个选项都可以根据需要使用。
答案 1 :(得分:0)
@Scadge答案解决了这个问题。我想添加use BigDecimal
而不是int
System.out.println((26851/48366)*100); //current condition gives 0
System.out.println((26851*100/48366)); //gives 55 but loss of precision
System.out.println((int) ((((double)26851)/48366)*100));//gives 55 but loss of precision
System.out.println(((((double)26851)/48366)*100)); //55.516271761154535 eligible but has own issues
BigDecimal b1=new BigDecimal(26851);
BigDecimal b2=new BigDecimal(48366);
BigDecimal b3=new BigDecimal(100);
System.out.println(b1.multiply(b3).divide(b2,2,RoundingMode.HALF_UP)); //55.52 more precise