質問

    Player Anfallare = new Player("A");
    Player Forsvarare = new Player("F");

    Anfallare.armees = 10;
    Forsvarare.armees = 10;
 do{
    Risk.status();

 }while(Anfallare.armees = 0 OR Forsvarare.armees = 0);

I would like to do something like this, that it should keep on status() until one of Anfallare.armees or Forsvarare.armees is 0.

This i made doesnt work though, i get required boolean, found int, how can i do this right?

役に立ちましたか?

解決

keep on status() until one of Anfallare.armees or Forsvarare.armees is 0

Since you want to run status() until one of those values is 0, you need to negate the whole thing:

do {
    Risk.status();
} while (Anfallare.armees != 0 && Forsvarare.armees != 0);

Translated this means: do status() while both the armees are non-zero -- so this has the same meaning as "until one of them is 0".

The condition could also be written as the negation of what you wrote:

while (! (Anfallare.armees == 0 || Forsvarare.armees == 0) )

But I think the first version is easier to read...

他のヒント

Change your while-condition to

while(Anfallare.armees == 0 || Forsvarare.armees == 0)

In java "or" is || and test on equality is ==.

It should be:

do {
   ...
} while(Anfallare.armees == 0 || Forsvarare.armees == 0);

The equality operator is == and not = (that is the assignment operator). Also, a logical OR in Java is represented by || and not OR.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top