Ejercicio
Repetir hasta 0
Objetivo
Cree un programa en java para pedir al usuario un número "x" y mostrar 10*x. Debe repetirse hasta que el usuario ingrese 0 (usando "while").
Código de Ejemplo
import java.util.Scanner; // Import Scanner class for input
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Declare a variable to store the number entered by the user
int x;
// Start a while loop that continues as long as x is not 0
while (true) {
// Ask the user to input a number
System.out.print("Enter a number (0 to stop): ");
// Read the input from the user
x = scanner.nextInt();
// If the user enters 0, break the loop and stop the program
if (x == 0) {
break;
}
// Display the result of 10 * x
System.out.println("10 * " + x + " = " + (10 * x));
}
// Close the scanner to free up resources
scanner.close();
}
}