Java 需要在程序中使用ArrayList的帮助吗?
看来,20个团正在不断形成。第一个团有1000人,第二个团有950人,第三个团有900人,依此类推到第二十团,第二十团只驻扎了50人。每周,每个团增加100人,到周末,最大的一个团被派到前线,总共持续了20周 对于这个项目,我已经设法打印出每个团的原始人数。但是我很难给每个团增加100人。增加人肯定是军队课堂上的一种方法。我正在使用.txt文件获取军团对象。这些档案中只包含编号为1-20的兵团名称Java 需要在程序中使用ArrayList的帮助吗?,java,Java,看来,20个团正在不断形成。第一个团有1000人,第二个团有950人,第三个团有900人,依此类推到第二十团,第二十团只驻扎了50人。每周,每个团增加100人,到周末,最大的一个团被派到前线,总共持续了20周 对于这个项目,我已经设法打印出每个团的原始人数。但是我很难给每个团增加100人。增加人肯定是军队课堂上的一种方法。我正在使用.txt文件获取军团对象。这些档案中只包含编号为1-20的兵团名称 public class Regiment { private String name;
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());
}
公营团{
私有字符串名称;//兵团名称
私有整数regNumber;//团号
二等兵;//团员
/**
*创建一个团对象。
*
*@param regNumber团号
*@param name该团的名称
*@param men一个团的人数
*/
公共团(国际注册号、字符串名称、国际人员){
this.name=名称;
this.regNumber=regNumber;
这个男人=男人;
}
/**
*返回团的名称。
*
*@返回团名
*/
公共字符串getName(){
返回名称;
}
/**
*返回团的编号。
*
*@返回团编号
*/
public int getregNumber(){
返回注册表号;
}
/**
*返回一个团中的人数。
*
*@团里的返回者
*/
公营机构{
返回人;
}
/**
*计算一个团的人数
*/
公共int地址2(int RegNumber){
int men=1050-(注册号*50);
返回人;
}
}
军医{
public ArrayList list;//军团对象列表
/**
*创建一个空列表
*/
公共军事助理(){
列表=新的ArrayList();
}
/**
*将团对象附加到列表中。
*
*@param current要附加到列表中的对象
*/
公共无效地址列表(当前团){
列表。添加(当前);
}
/**
*将一个团对象删除到列表中。
*
*@param current要从列表中删除的对象
*/
公共作废移除列表(团当前){
列表。删除(当前);
}
/**
*获得了最大的兵团。
*/
公营团{
if(list.isEmpty()){
返回null;
}
军团Reg1=list.get(0);
对于(int i=1;i最大吗
if(current.getMen()>Reg1.getMen()){
Reg1=电流;
}
}
返回Reg1;
}
/**
*每个团增加兵力。
*/
公共无效地址(){
}
/**
*将列表转换为多行字符串,每行包含
*一个团的数据。
*
*@返回包含列表中所有数据的字符串
*/
公共字符串toString(){
串出
=String.format(“%28s%12s%n”、“团”、“人”)
+String.format(“%12s%n”,“Number”)
+字符串格式(“%12s%16s%14s%n”,”=========================================================================”,
"=========");
对于(int i=0;i
您可以编写addNewMen()在兵团课上,你可以增加100人,因为兵团是兵团的财产。有什么问题吗?你有什么问题吗?你犯了什么错误?你读过关于如何提出一个好问题的帮助部分吗?或者你只是想让人帮你做作业吗?@MattCoubrough是的,我什么都不想做,但不想做莫奇:DNo。我没有犯任何错误。我提到的唯一问题是我不知道如何为每个团增加人手。我只想帮助你理解我应该做什么。你能给我任何其他帮助吗?谢谢,在陆军课上有没有办法做到这一点。没有,因为它以团名单为变量。人属于团。