0

So this program asks for different areas of shapes, and it was requested that the user be able to control whether they want the program to continue or stop. I created an if statement, within the while loop that if the user entered "stop" the program would break. However, after testing the program is continuing to run. How do I break the while loop?

import java.util.Scanner;
public class assignmentu3b {
static int area;
static String stop;
    public static void main(String[]args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Would you like to find the area of a rectangle,circle or triangle");
    String inp=sc.next();
    inp.toLowerCase();
    while(true) {
        System.out.println("Enter stop, to leave the program, or anythinng else to continue, the program will run once.");
        stop=sc.next();
        stop.toLowerCase();
        if(stop=="stop") {
            break;
        }
    switch(inp) {
    case "circle":
        area=circle(area);
        System.out.println("The area of the circle is "+area);
        break;
    case "rectangle":
        area=rectangle(area, area);
        System.out.println("The area of the rectangle is "+area);
    break;
    case "triangle":
        area=triangle(area, area);
        System.out.println("The area of the triangle is "+area);
        break;
        default:
            System.out.println("You didn't enter a shape");
    }
    
}
}

public static int circle(int radius) {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter a radius:");
    radius=sc.nextInt();
    return (int) (Math.PI*Math.pow(radius,2));
    
}
public static int rectangle(int lenght,int width) {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter a lenght and width");
    lenght=sc.nextInt();
    width=sc.nextInt();
    return lenght*width;
}

public static int triangle(int base,int height) {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter a base and height");
    base=sc.nextInt();
    height=sc.nextInt();
    return (base*height)/2;
    
}

}

0 Answers0