Answer the question
In order to leave comments, you need to log in
How to set up the counter?
In the proposed code from Eckel, I wanted to calculate the serial number of generating a number, until the moment when it satisfies the condition.
The total number of hits gives out, but I need the counter to increase by 1 after each hit.
package study.it.java.Eccel000077;
public class Eccel000077p130loopWhile {
static boolean condition() {
double rndm = Math.random();
System.out.println(rndm);
boolean result = rndm < 0.99;
System.out.println(result + ", ");
return result;
}
public static void main(String[] args) {
int counter = 0;
while (condition())
counter++;
System.out.println(counter);
System.out.println("Inside 'while'");
System.out.println("Exited 'while");
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question