Problem With An Else Statement - Probably Doing Something Simple Wrongly
At line 22, the compiler is saying that there is an else statement without an if? How come? The if is there.
Its a program designed to calculate the area of a circle, rectangle or triangle depending on the user's needs.
I've only gone as far a circle as I feel if I get circle done then the others shouldn't be a huge problem as they should (HOPEFULLY) be a case of repetition. Sorry for all the comments in the code, that's howe have to present our work.
Code:
// Program to read in a shape from a user, using JOptionPane, then ask for measurement again using
//JOptionPane then calculate the area
import javax.swing.JOptionPane; //import JOptionPane
public class CircleRectangleTriangle //create class name
{ //open class
public static void main (String[] args) //create args
{ //open args
int higher, lower; //declare ints
String circle=circle; //String with value circle
String rectangle=rectangle; //String with value rectangle
String triangle=triangle; //String with value triangle
String shape=JOptionPane.showInputDialog(null, "Do you want to calculate the area of a circle, rectangle or triangle?:"); //receive shape type
if (shape.equals(circle)) //if shape string equals circle string
{ //open if
String userinput=JOptionPane.showInputDialog(null, "Enter the value of the radius:"); //receive radius value
radius=Double.parseDouble(userinput); //Parses userinput string to a double
if (radius < 0); //If radius value is lower than 0
{ //open if
JOptionPane.showMessageDialog(null, "Radius value less than 0: cannot compute circle area"); //Inform user of error
} //close if
else //else
{ //open else
area = radius*radius*3.1416; //calculate size of area
JOptionPane.showMessageDialog(null, "A circle of radius " + radius + " has an area of " + area + "."); //Inform user of size of area
} //close else
} //close if
System.exit(0); //program terminates safely
} //close args
} //close class
Re: Problem With An Else Statement - Probably Doing Something Simple Wrongly
You if statement ends in a semicolon.
if(whatever); //nothing happens
{
///always executes
}
else{
///else without if
}
Re: Problem With An Else Statement - Probably Doing Something Simple Wrongly
Great, thanks! Still get confused over syntax.
Re: Problem With An Else Statement - Probably Doing Something Simple Wrongly