Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda