I am new to java. i created a program to find the area of polygons. I wanted it to ask the type of polygon an then find the area. i used if, else if and else statements but whenever i type the name of the polygon nothing happens.
Here is the script
import java.util.Scanner ;
public class Area
{
static Scanner sc = new Scanner(System. in );
public static void main(String[] args) {
System.out.print("Enter the type of polygon: ");
String polygon = new String("polygon");
polygon = sc.next();
String square = new String("square");
String rectangle = new String("rectangle;");
String triangle = new String("triangel");
polygon = sc.next();
if (polygon == square) {
double side;
System.out.print("Enter the side: ");
side = sc.nextDouble();
double area;
area = side * side;
System.out.print("The Area is: " + area);
} else if (polygon == rectangle) {
double length;
double breadth;
System.out.print("Enter the length: ");
length = sc.nextDouble();
System.out.print("Enter the breadth: ");
breadth = sc.nextDouble();
double area;
area = length * breadth;
System.out.print("The Area is : " + area);
} else if (polygon == triangle) {
double base;
double height;
System.out.print("Enter the base: ");
base = sc.nextDouble();
System.out.print("Enter the height: ");
height = sc.nextDouble();
Double area;
area = base * height / 2;
System.out.print("The Area is: " + area);
} else {
System.out.print("ERROR it is not a polygon");
}
}
}
Please help me out thank you