我能否在我的代码中使用这个for循环来提取不同的值?

huangapple 未分类评论43阅读模式
英文:

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 &lt; 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 = &quot;New &quot; + 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() + &quot; has been made.&quot;);
            }
            else if (citiesInfected.getValue(i).getInfectionLevel() == 4) {
                citiesQuarantined.add(citiesInfected.getValue(i));
                System.out.println(citiesInfected.getValue(i).getCityName() + &quot; has been placed in 
quarantine!&quot;);
            }
            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

huangapple
  • 本文由 发表于 2020年4月5日 09:15:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/61036875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定