Note that there are some explanatory texts on larger screens.

plurals
  1. POmethod doesn't finish running - works infinitly
    primarykey
    data
    text
    <p>I tried to write simple program and stuck at some point into while loop.</p> <p>From current output I see that it only print the same data.</p> <p>But it should be executed at every iteration.</p> <p><strong>Snippet of code</strong>:</p> <pre><code>public void fight(Droid first, Droid second) { while (first.isAlive() &amp; second.isAlive()) { if ((random.nextInt(10) + 1) % 2 == 0) { shoot(first, second); } else { shoot(second, first); } // testing output System.out.printf("Droid %s has %d power level%n", first.getName(), first.getPower()); System.out.printf("Droid %s has %d power level%n", second.getName(), second.getPower()); } if (first.getPower() &lt;= 0) { fightResult(second); } else { fightResult(first); } } </code></pre> <p><strong>And here is output</strong>:</p> <pre><code>Droid John has 5 power level Droid Dik has 8 power level Droid John has 5 power level Droid Dik has 8 power level Droid John has 5 power level Droid Dik has 8 power level Droid John has 5 power level Droid Dik has 8 power level </code></pre> <p>I made some silly mistake and couldn't find one.</p> <p><strong>Update:</strong></p> <p>here is <code>shoot()</code>:</p> <pre><code>private void shoot(Droid first, Droid second) { second.setPower(first.getImpact()); } </code></pre> <p>and <code>Droid</code> class:</p> <pre><code>public abstract class Droid { protected Random random; private String name; private int power; private int impact; public Droid(int aPower, int anImpact) { power = aPower; impact = anImpact; } public String getName() { return name; } public void setPower(int aPower) { power = aPower; } public void setImpact(int anImpact) { impact = anImpact; } public int getPower() { return power; } public int getImpact() { return impact; } public abstract void fight(Droid first, Droid second); public boolean isAlive() { boolean result = false; if (this.getPower() &gt; 0) result = true; return result; } } </code></pre> <ul> <li><strong>How to solve this trouble?</strong></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload