似乎有20个团在不断形成的过程中。第一组有1000名男子,第二名有900名,第三名有900名,依此类推至第二十二团,只有50人。每周都有100名男子加入每个团,并在周末,最大的团被派往前线。这持续了20个星期。
对于这个程序,我已经设法打印出每个团的原始人数。但是我很难在每个团中增加100名士兵。增兵必须是军队中的一种方法。我使用.txt文件获取了regiment对象。所有这些文件都包含编号为1-20的团的名称。
我目前没有错误我唯一的问题是我不知道如何将男性加入我的团。我必须在军队类中使用addMen方法,我目前有空白。
public class Regiment {
private String name; //name of regiment
private int regNumber; //regiment number
private int men; // regiment men
public Regiment(int regNumber, String name, int men) {
this.name = name;
this.regNumber = regNumber;
this.men = men;
}
public String getName() {
return name;
}
public int getregNumber() {
return regNumber;
}
public int getMen() {
return men;
}
public int addMen2(int RegNumber) {
int men = 1050 - (regNumber * 50);
return men;
}
}
ArmyDataList:
class ArmyDataList {
public ArrayList<Regiment> list;
public ArmyDataList() {
list = new ArrayList<Regiment>();
}
public void AddToList(Regiment current) {
list.add(current);
}
public void RemoveFromList(Regiment current) {
list.remove(current);
}
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;
}
public void addMen() {
}
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";
}
}
RegimentTest:
public class RegimentTest {
public static void main(String[] args) throws IOException
{
ArmyDataList army = new ArmyDataList();
Scanner fileScan = new Scanner(new File("regiments.txt"));
System.out.println("Report Summary:\n");
while (fileScan.hasNext()) {
String line = fileScan.nextLine();
System.out.println(line);
Scanner in = new Scanner(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
Regiment adder = new Regiment(regNumber, name, men );
army.AddToList(adder) ;
}
System.out.println(army.toString());
}
答案 0 :(得分:0)
在setMen(int numberOfMen)
课程中添加Regiment
方法。然后在addMen()
方法中,您可以执行以下操作:
public void addMen(){
for(Regiment r : list){ //iterate through the list of regiments
r.setMen(r.getMen() + 100); //add 100 men to each regiment
}
}
setMen
方法如下所示:
public void setMen(int numberOfMen){
men = numberOfMen;
}
你的toString
方法存在另一个问题,即调用团的addMen2
方法 - 现在你只是打印数字,而不是初始化人数。在Regiment
类的构造函数中,替换
this.men = men;
带
this.men = addMen2(regNumber);
然后在您的toString
方法中,替换
int men = regim.addMen2(regNumber);
带
int men = regim.getMen();
以下是main
应该是这样的内容:
public static void main(String[] args) throws IOException{
ArmyDataList army = new ArmyDataList();
Scanner fileScan = new Scanner(new File("regiments.txt"));
System.out.println("Report Summary:\n");
while (fileScan.hasNext()) {
String line = fileScan.nextLine();
System.out.println(line);
Scanner in = new Scanner(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
Regiment adder = new Regiment(regNumber, name, men );
army.AddToList(adder);
}
System.out.println(army.toString()); //print out the initial # of men
for(int i = 0; i < 20; i++)
army.addMen();
System.out.println(army.toString()); //print the final # of men
}
答案 1 :(得分:0)
Regiment
中的删除方法addMen2
,并将其替换为
public void addMen(int men) {
this.men +=men;
}
然后在你的军队中你可以有方法
public void addMen(int men) {
for(Regiment regiment : list){
regiment.addMen(men);
}
}
这将是向每个团队增加100人的最简单的解决方案,
另外一点是,你的toString
有点讨厌,军团应该知道它有多么狡猾的士兵,你不需要额外的方法来计算它(我推荐你去垃圾addMen2
方法的原因)< / p>
发起你的Regiment
,使用构造函数。你想拥有1000,1950,1900等规模的军团,在你创造它们的时候去做
while (fileScan.hasNext()) {
String line = fileScan.nextLine();
System.out.println(line);
Scanner in = new Scanner(line) ;
int regNumber = in.nextInt();
String name = in.next();
int men = 1050 - (regNumber * 50);
Regiment adder = new Regiment(regNumber, name, men );
army.AddToList(adder) ;
}