java do-while loop with user input
java do-while loop with user input
Here's an example of a Java do-while
loop that takes user input:
import java.util.Scanner;
public class UserInputDoWhile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter a positive integer: ");
if (scanner.hasNextInt()) {
number = scanner.nextInt();
if (number > 0) {
System.out.println("You entered: " + number);
break; // Exit the loop
} else {
System.out.println("Please enter a positive integer.");
}
} else {
System.out.println("Invalid input. Please try again.");
scanner.next(); // Clear the invalid input
}
} while (true);
System.out.print("Now, enter your favorite color (1-3): ");
int choice;
do {
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
if (choice >= 1 && choice <= 3) {
switch (choice) {
case 1:
System.out.println("You chose Red.");
break;
case 2:
System.out.println("You chose Green.");
break;
case 3:
System.out.println("You chose Blue.");
break;
}
break; // Exit the loop
} else {
System.out.println("Please enter a number between 1 and 3.");
}
} else {
System.out.println("Invalid input. Please try again.");
scanner.next(); // Clear the invalid input
}
} while (true);
scanner.close();
}
}
Let's go through what this code does:
First, we create a Scanner
object called scanner
to read user input from the console.
Next, we have our first do-while
loop. We prompt the user to enter a positive integer. If the input is valid (i.e., an integer greater than 0), we print out a message and exit the loop using the break
statement. If the input is invalid (e.g., not an integer or negative), we print out an error message, clear the invalid input with scanner.next()
, and continue looping.
Once the user enters a valid positive integer, the program continues to the next prompt: "Now, enter your favorite color (1-3):". This time, we have another do-while
loop. We again read user input using scanner.nextInt()
and check if it's within the range of 1 to 3 (inclusive). If the input is valid, we use a switch
statement to print out a message based on the chosen color and exit the loop with break
. If the input is invalid, we print out an error message, clear the invalid input with scanner.next()
, and continue looping.
Finally, when the user enters a valid favorite color choice, the program prints out a corresponding message and then closes the Scanner
object to release system resources.
This code demonstrates how you can use do-while
loops in Java to repeatedly ask for user input until it meets certain criteria.
How to print 1 to 100 with while loop in Java?
I'm happy to help you with that! Here's a step-by-step guide on how to print the numbers from 1 to 100 using a while loop in Java:
Code:
public class PrintNumbers {
public static void main(String[] args) {
int number = 1;
while (number <= 100) {
System.out.println(number);
number++;
}
}
}
Let's break down the code:
We declare anint
variable number
and initialize it to 1. This will be our loop counter. The while
loop condition is number <= 100
, which means the loop will continue as long as number
is less than or equal to 100. Inside the loop, we print the current value of number
using System.out.println()
. We increment number
by 1 using the ++
operator. This is crucial, as it ensures that the loop continues until number
reaches 100.
How it works:
When you run this program, the output will be a list of numbers from 1 to 100, printed one after another:
1
2
3
...
98
99
100
Here's what happens behind the scenes:
Thenumber
variable is initialized to 1. The while loop condition is evaluated: since number
is less than or equal to 100, the loop body is executed. We print the current value of number
, which is 1. number
is incremented by 1 (i.e., becomes 2). The loop condition is re-evaluated: since number
is still less than or equal to 100, the loop body is executed again. We print the new value of number
, which is now 2. ...and so on.
The loop continues until number
reaches 100, at which point the condition becomes false, and the loop exits.
Tips and Variations:
You can modify the code to print numbers in reverse order (from 100 to 1) by changing the loop condition tonumber >= 1
. To print only even or odd numbers, you can add a conditional statement inside the loop: for example, if (number % 2 == 0) System.out.println(number);
to print only even numbers. If you want to print something more complex, like a countdown timer, you can adjust the loop condition and the output statements accordingly.
I hope this helps! Let me know if you have any questions or need further clarification.