Hi there strongbow.
I've written the program for you. I've included comments, so it should
be fairly self-explanatory, but if anything is unclear, then feel free
to request clarification before rating this answer.
You can download it from: http://xult.org/Triangle.java
Or you can copy and paste from below.
Good luck
--seizer-ga
To compile this code, simply type at the command line:
javac Triangle.java
To run it, type:
java Triangle
-------- CODE IS BELOW THIS LINE --------
// We need to import this for user input
import java.io.*;
public class Triangle {
public static void main(String args[]) {
// declare our variables
int side1=0, side2=0, side3=0;
// Read in the lengths of the sides
try {
side1 = Integer.parseInt(getInput("Please enter the length of the
first side: "));
side2 = Integer.parseInt(getInput("Please enter the length of the
second side: "));
side3 = Integer.parseInt(getInput("Please enter the length of the
third side: "));
} catch (NumberFormatException e) {
// We might get this error if the user types in a string, rather
than a valid integer
System.out.println("Error: " + e.getMessage());
System.exit(1);
} catch (IOException e) {
// We might get this error if there was a problem reading from the
keyboard
System.out.println("Error: " + e.getMessage());
System.exit(1);
}
// Here we do a sanity check. If the user does not type in three
valid sides, we warn them and exit
if (side1 + side2 + side3 != 180) {
System.out.println("Warning: sides should add up to 180 to be a
valid triangle");
System.exit(1);
}
/*
* Now we determine which triangle it is.
*
* If side1 is equal to side2, AND side2 is equal to side3, we know
that
* side1 must be equal to side3 because of the rule of inference.
This
* means that the triangle is equilateral.
*
* If side1 is equal to side2, OR side1 is equal to side3, OR side2
is equal to
* side3, then the triangle fits the criteria for being an
isosceles.
*
* If neither of the above are true, then we know that the triangle
must be
* scalene.
*
*/
if (side1 == side2 && side2 == side3) {
System.out.println("Equilateral");
System.exit(0);
} else if (side1 == side2 || side1 == side3 || side2 == side3) {
System.out.println("Isosceles");
System.exit(0);
} else {
System.out.println("Scalene");
System.exit(0);
}
}
// We use this method to obtain console input from the user.
public static String getInput(String question) throws IOException {
// Display the question to the user
System.out.print(question);
// obtain their input
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
// return that input
return in.readLine();
}
}
-------- CODE IS ABOVE THIS LINE -------- |