150k views
4 votes
Java programminh

a-Write a while loop that ensures the user enters a positive integer number (named amount). Prior to the loop, is the code below. Write the loop below it.

int amount;

System.out.print ("Enter a positive integer: ");

amount = scan.nextInt ();

b. Write a for loop to print the multiples of 3 from 300 down to 3 (i.e., 300, 297, 294, etc.), each value on a different line (next line down).

1 Answer

2 votes

Final answer:

Using a while loop ensures that the user inputs a positive integer for the variable amount. A for loop is written to print the multiples of 3 from 300 down to 3, each on a new line.

Step-by-step explanation:

To ensure the user enters a positive integer number for amount, a while loop is used. The loop continues to prompt the user until they enter a positive integer:

int amount;
System.out.print ("Enter a positive integer: ");
amount = scan.nextInt();
while (amount <= 0) {
System.out.print("Please enter a positive integer: ");
amount = scan.nextInt();
}

For printing the multiples of 3 from 300 down to 3, a for loop can be used:

for (int i = 300; i >= 3; i -= 3) {
System.out.println(i);
}
User Molarro
by
7.7k points