197k views
0 votes
Jump to level 1 Integer numinput is read from input. Write a while loop that iterates until numInput is negative. At each iteration: - Update integer finalVal as follows: - If numinput is divisible by 3 , output "lose" and do not update finalVal. - Otherwise, output "win" and increment finalVal. - Then, read an integer from input into variable num input. End each output with a newline. Ex: If the input is 793−1, then the output is: win lose lose Final value is 1 Note: x%3==0 returns true if x is divisible by 3 .

User Mitchkman
by
8.1k points

1 Answer

5 votes

Final answer:

The student's question involves writing a while loop that iterates until an integer input becomes negative, checking for divisibility by 3, and updating a counter accordingly.

Step-by-step explanation:

The student's question is based on the concept of using a while loop to perform certain actions based on a given integer's properties. Here's how you can structure the while loop to meet the requirements:

int finalVal = 0; // Initialize finalVal to 0 before entering the loop
while(numinput >= 0) { // Continue the loop until numinput is negative
if(numinput % 3 == 0) { // Check if numinput is divisible by 3
System.out.println("lose");
} else {
System.out.println("win");
finalVal++; // Only increment finalVal if numinput is not divisible by 3
}
numinput = // read next integer from input here
}
System.out.println("Final value is " + finalVal);

Each iteration of the loop checks whether the numinput is divisible by 3. If it is, it prints "lose" and does not change finalVal. If it is not divisible by 3, it prints "win" and increments finalVal. The loop continues to iterate until numinput becomes negative.

User Tuffy
by
8.6k points