I am a class 9 student, and I wanted to know how to include strings in if statements Please help
Any sort of help would be lovely just tell me what to do
Thanks in advance!
The code is a bit long but it is just the statements I am using BlueJ
if you can Please do explain your answers
I do believe the problem is in the shape== line
import java.util.*;
public class AreaOf2D
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
//to calculate area of 2d figures
System.out.println("Area Calculator");
System.out.println(" ");
System.out.print("Enter the Shape: ");
String Shape = input.nextLine();
System.out.println(" ");
//Condition Statements
if(Shape=="triangle")
{
//base
System.out.print("Enter the Base: ");
double BaseTri = input.nextDouble();
System.out.println(" ");
//height
System.out.print("Enter the Height: ");
double HeightTri = input.nextDouble();
//The formulaes
double tri = ((1/2) * BaseTri * HeightTri);
//answer
System.out.print("The Area of the triangle is: " + tri);
}
if(Shape=="square")
{
//Side
System.out.print("Enter the Side: ");
double Side = input.nextDouble();
System.out.println(" ");
//formulae
double sqr = (Math.pow(Side,2));
//answer
System.out.print("The Area of the Square is: " + sqr);
}
if(Shape=="rectangle")
{
//Length
System.out.print("Enter the Length: ");
double Length = input.nextDouble();
System.out.println(" ");
//Height
System.out.print("Enter the Height: ");
double Height = input.nextDouble();
//formulaes
double rec = ((Length * Height));
//answer
System.out.print("The Area of the Rectangle is: " + rec);
}
if(Shape=="rhombus")
{
//Diagonal1
System.out.print("Enter the first Diagonal: ");
double Diagonal1 = input.nextDouble();
System.out.println(" ");
//Diagonal2
System.out.print("Enter the second Diagonal: ");
double Diagonal2 = input.nextDouble();
//formulae
double rho = ((1/2) * Diagonal1 * Diagonal2);
//answer
System.out.print("The Area of the Rhombus is: " + rho);
}
if(Shape=="parallelogram")
{
//BasePar
System.out.print("Enter the Base: ");
double BasePar = input.nextDouble();
System.out.println(" ");
//Diagonal2
System.out.print("Enter the second Diagonal: ");
double HeightPar = input.nextDouble();
//formulae
double par = ( BasePar * HeightPar);
//answer
System.out.print("The Area of the Parallelogram is: " + par);
}
if(Shape=="kite")
{
//Diagonal1kit
System.out.print("Enter the first diagonal: ");
double Diagonal1kit = input.nextDouble();
System.out.println(" ");
//Diagonal2kit
System.out.print("Enter the second Diagonal: ");
double Diagonal2kit = input.nextDouble();
//formulae
double kit = ((1/2) * Diagonal1kit * Diagonal2kit);
//answer
System.out.print("The Area of the Kite is: " + kit);
}
}
}
The output is:
Area Calculator
Enter the Shape:
And when I enter the name nothing happens no code is run!
Aucun commentaire:
Enregistrer un commentaire