Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
How can I make the program stop after entering a value from a user?

I am trying to write a program that receives the number of sides from the user and determines the type of figure using switch structure and a while sentinel-controlled loop, but every time I get an infinite loop. How can that be fixed?

import java.util.Scanner;
public class P1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the number of sides:");
        int s = input.nextInt();
        while ( s!=-1)
        {
            switch (s)
            {
            case 1: System.out.println("Line");
                break;
            case 2:System.out.println("Angle");
                break;
            case 3:System.out.println("Triangle");
                break;
            case 4:System.out.println("Quadrilateral");
                break;
            case 5:System.out.println("Pentagon ");
                break;
            case 6:System.out.println("Hexagon");
                break;
            case 7:System.out.println("Heptagon");
                break;
            case 8:System.out.println("Octagon");
                break;
            case 9:System.out.println("Nonagon");
                break;
            case 10:System.out.println("Decagon");
                break;
            default: System.out.println("Enter a valid value:");
            }   
        }
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The while loop is written to continue as long as s!=-1; so you need to change s so that this expression is no longer true.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda