-
int i;
i = 0;
while (i < 5) {
StdOut.print(i);
i = i + 1;
}
-
int i;
i = 0;
while (i < 10) {
StdOut.println(i);
i = i + 2;
}
-
int i;
i = 0;
while (i < 10) {
StdOut.print(i + " ");
StdOut.println((int) Math.pow(i,2));
i = i + 1;
}
-
This code tries to print out all the integers up to 100. Something is wrong. What
is the bug?
int i;
i = 0;
while (i <= 100) {
StdOut.println(i);
}
-
How many asterisks get printed by the following code segment?
int i;
i = 10;
while (i <= 77) {
StdOut.print("*");
i = i + 1;
}
-
Write a Java while-loop that prints numbers in reverse starting from 50
going down to -2.
-
Answer questions about the following code segment.
int num, count, sum;
count = 0;
sum = 0;
num = StdIn.readInt();
while (num != -999) {
StdOut.print("Enter a number: ");
sum = sum + num;
count = count + 1;
num = StdIn.readInt();
}
StdOut.print((double) sum/count);
- When and how does the loop termintate?
- What does the code segment compute?
-
If the final print statement were to be:
StdOut.print(sum/count);
Is the program still correct? Why or why not?
-
Use a while loop to generate 100 filled circles in random locations
in a 500-by-500 window. Circles should have random colors as well as random
diameters between 20 and 50 pixels.