-1

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:");
            }   
        }
    }
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
KAM30
  • 1
  • 1

1 Answers1

1

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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101