英文:
Am I able to do this for loop in my code to pull out different values?
问题
以下是翻译好的内容:
public static void checkInf() {
for (int i = 0; i < citiesInfected.length(); i++) {
if (citiesInfected.getValue(i).getInfectionLevel() == 1) {
citiesInfected.remove(i);
City newPop = citiesInfected.getValue(i);
int popRounded = (int) (newPop.getPopulation() * .9);
int realPopRound = popRounded + 1;
newPop.setPopulation(realPopRound);
citiesInfected.add(citiesInfected.getValue(i));
} else if (citiesInfected.getValue(i).getInfectionLevel() == 2) {
citiesInfected.remove(i);
City newPop = citiesInfected.getValue(i);
int popRounded = (int) (newPop.getPopulation() * .8);
int realPopRounded = popRounded + 1;
newPop.setPopulation(realPopRounded);
citiesInfected.add(citiesInfected.getValue(i));
} else if (citiesInfected.getValue(i).getInfectionLevel() == 3) {
citiesInfected.remove(i);
City newPop = citiesInfected.getValue(i);
int popRounded = (int) (newPop.getPopulation() * .75);
int realPopRound = popRounded + 1;
String newName = "New " + citiesInfected.getValue(i).getCityName();
newPop.setPopulation(realPopRound);
int pop = (int) (citiesInfected.getValue(i).getPopulation() * .75);
int populationRounded = pop + 1;
int newInfectionLevel = 0;
City newCity = new City(newName, populationRounded, newInfectionLevel);
citiesInfected.add(newCity);
citiesInfected.add(citiesInfected.getValue(i));
System.out.println(newCity.getCityName() + " has been made.");
} else if (citiesInfected.getValue(i).getInfectionLevel() == 4) {
citiesQuarantined.add(citiesInfected.getValue(i));
System.out.println(citiesInfected.getValue(i).getCityName() + " has been placed in quarantine!");
} else return;
}
}
输入城市数据及其感染等级:
b,2310,2
r,1967,1
e,3167,2
n,781,0
d,6706,0
a,2902,2
nn,8234,3
期望的输出:
New b has been made.
New e has been made.
New a has been made.
英文:
For my code I am trying to keep track of infection levels for different cities for a homework assignment. When I add one to these values for some reason they don't workout when I run it through this for loop. I am wondering how I could fix this code to make it work and run better. Here is my main for loop that runs the main part I am trying to fix.
public static void checkInf() {
for (int i = 0; i < citiesInfected.length(); i++) {
if(citiesInfected.getValue(i).getInfectionLevel() == 1){
citiesInfected.remove(i);
City newPop = citiesInfected.getValue(i);
int popRounded = (int) (newPop.getPopulation() * .9);
int realPopRound = popRounded + 1;
newPop.setPopulation(realPopRound);
citiesInfected.add(citiesInfected.getValue(i));
}
else if (citiesInfected.getValue(i).getInfectionLevel() == 2) {
citiesInfected.remove(i);
City newPop = citiesInfected.getValue(i);
int popRounded = (int) (newPop.getPopulation() * .8);
int realPopRounded = popRounded +1;
newPop.setPopulation(realPopRounded);
citiesInfected.add(citiesInfected.getValue(i));
}
else if (citiesInfected.getValue(i).getInfectionLevel() == 3) {
citiesInfected.remove(i);
City newPop = citiesInfected.getValue(i);
int popRounded = (int) (newPop.getPopulation() * .75);
int realPopRound = popRounded + 1;
String newName = "New " + citiesInfected.getValue(i).getCityName();
newPop.setPopulation(realPopRound);
int pop = (int) (citiesInfected.getValue(i).getPopulation() * .75);
int populationRounded = pop + 1;
int newInfectionLevel = 0;
City newCity = new City(newName, populationRounded, newInfectionLevel);
citiesInfected.add(newCity);
citiesInfected.add(citiesInfected.getValue(i));
System.out.println(newCity.getCityName() + " has been made.");
}
else if (citiesInfected.getValue(i).getInfectionLevel() == 4) {
citiesQuarantined.add(citiesInfected.getValue(i));
System.out.println(citiesInfected.getValue(i).getCityName() + " has been placed in
quarantine!");
}
else return;
}
}
I am giving it these inputs for the cities the last numbers being the infection levels.
b,2310,2
r,1967,1
e,3167,2
n,781,0
d,6706,0
a,2902,2
nn,8234,3
When I add one to all of those last numbers I want each of the numbers to do a different action(the actions being the ones in the for loop). Basically I am just confused if my for loop is messed up which is why I am getting the wrong input, or if it is something different. I really hope this makes sense. Here is also the output of my code.
The infection level for all cities has been increased.
New r has been made.
New n has been made.
nn has been placed in quarantine!
I want it to say:
New b has been made.
New e has been made.
New a has been made.
Because all of these are at infection level 2
专注分享java语言的经验与见解,让所有开发者获益!
评论