看來20個團正在形成一個連續的過程。第一個有1000人,第二個有950個,第三個有900個,等到第二十個團只有50個。在每個星期裏,每個團增加了100個人,而在本週結束時,最大的團是被送到前線。這持續了20周。在程序中使用數組列表需要幫助嗎?
對於這個程序,我已經設法打印出每個團的原始人數。但是,我很難在每個團裏增加100個人。增加的人必須是軍隊級的一種方法。我正在使用.txt文件獲取團體對象。所有這些文件包含的編號爲1-20的團的名字。
public class Regiment {
private String name; //name of regiment
private int regNumber; //regiment number
private int men; // regiment men
/**
* Creates a Regiment object.
*
* @param regNumber the regiment number
* @param name the name of the regiment
* @param men the number of men in a regiment
*/
public Regiment(int regNumber, String name, int men) {
this.name = name;
this.regNumber = regNumber;
this.men = men;
}
/**
* Returns the name of the regiment.
*
* @return the regiment name
*/
public String getName() {
return name;
}
/**
* Returns the number of the regiment.
*
* @return regiment number
*/
public int getregNumber() {
return regNumber;
}
/**
* Returns the number of men in a regiment.
*
* @return men in regiment
*/
public int getMen() {
return men;
}
/**
* Computes the number of men in a regiment
*/
public int addMen2(int RegNumber) {
int men = 1050 - (regNumber * 50);
return men;
}
}
class ArmyDataList {
public ArrayList<Regiment> list; // list of regiment objects
/**
* Creates an empty list
*/
public ArmyDataList() {
list = new ArrayList<Regiment>();
}
/**
* Appends a regiment object to the list.
*
* @param current the object to be appended to the list
*/
public void AddToList(Regiment current) {
list.add(current);
}
/**
* Removes a regiment object to the list.
*
* @param current the object to be removed from the list
*/
public void RemoveFromList(Regiment current) {
list.remove(current);
}
/**
* Gets the largest regiment based on men.
*/
public Regiment getLargest() {
if (list.isEmpty()) {
return null;
}
Regiment Reg1 = list.get(0);
for (int i = 1; i < list.size(); i++) {
Regiment current = list.get(i); // get next regiment
// is current regiment > largest
if (current.getMen() > Reg1.getMen()) {
Reg1 = current;
}
}
return Reg1;
}
/**
* Adds men to each regiment.
*/
public void addMen() {
}
/**
* Converts the list to a multi-line string, with each line containing the
* data for one regiment.
*
* @return the String containing all the data on the list
*/
public String toString() {
String out
= String.format("%28s%12s%n", "Regiments", " Men")
+ String.format("%12s%n", "Number")
+ String.format("%12s%16s%14s%n", "=======", "===============",
"=========");
for (int i = 0; i < list.size(); i++) {
Regiment regim = list.get(i);
int regNumber = regim.getregNumber();
String name = regim.getName();
int men = regim.addMen2(regNumber);
out = out + String.format("%12s", regNumber)
+ String.format("%16s", name)
+ String.format("%10s", men)
+ "\n";
}
return out + "\n";
}
}
public class RegimentTest {
public static void main(String[] args) throws IOException
{
ArmyDataList army = new ArmyDataList();
// create Scanner object to read each line of file until eof
Scanner fileScan = new Scanner(new File("regiments.txt"));
System.out.println("Report Summary:\n");
while (fileScan.hasNext()) // while not eof...
{
// read next line
String line = fileScan.nextLine();
// "echo print" data entered
System.out.println(line);
// 1. create a Scanner object
Scanner in = new Scanner(line) ;
// 2. extract tokens from current line
int regNumber = in.nextInt();
String name = in.next();
int men = 0 ; //men is set to 0 only because I havent add the men yet
// 3. create Regiment object passing the tokens to the constructor
Regiment adder = new Regiment(regNumber, name, men);
// 4. add object to list
army.AddToList(adder) ;
}
System.out.println(army.toString());
}
問題是什麼?你有什麼麻煩?你得到什麼錯誤?你有沒有閱讀關於如何提出一個好問題的幫助部分?或者你只是想讓別人爲你做作業? – 2014-12-07 23:05:26
@MattCoubrough是的,我想什麼都不做,但不是很多:D – nbro 2014-12-07 23:06:40
不,我沒有得到任何錯誤我提到唯一的問題是我不知道如何給每個團隊加人。我只想幫助理解我應該做什麼。 – Name 2014-12-07 23:07:02