Results 1 to 9 of 9
  1. #1
    TheApostle is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default Is it a right triangle? (Code help)

    Hi! My task is to create a program that takes the input of three sides of a triangle and displays a message of whether or not it is a right triangle.

    Here is what I have so far...
    ------------------------------
    import java.util.Scanner;
    public class triangle {

    public static void main(String[] args) {

    double sidea;
    double sideb;
    double sidec;
    String answer;

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the 3 sides of the triangle: ");

    sidea = in.nextDouble();
    sideb = in.nextDouble();
    sidec = in.nextDouble();

    //if it is a right triangle
    if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec));
    answer = "right triangle";
    if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb));
    answer = "right triangle";
    if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea));
    answer = "right triangle";

    System.out.print(answer);


    }

    }

    ------------------------------------------------------

    I need help making an else statement that says (if all that "if" statement above is NOT true, then it is NOT a right triangle)!

    Thanks!!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Java Code:
    if (A | B | C) {
       true;
    }else{
       false;
    }

  3. #3
    TheApostle is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Keeps saying that line 29 (true;) and line 31 (false;) are not statements : \

  4. #4
    TheApostle is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    sorry those smileys were supposed to be semicolons?- Was I supposed to put that code in underneath my System.out.print?
    Last edited by TheApostle; 10-07-2008 at 07:11 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    If you get compile errors, copy FULL text of the error messages here.
    I don't know where line 29 and 31 are.


    The code I posted was not java. It was just to show you how to use OR with an if statement.

  6. #6
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Norm gave you one way to solve this. Read up on how if-else works. I'd do it like this:

    Java Code:
    if (something) {
        // code
    } else if (something else) {
        // code
    } else {
        // code
    }
    Or you can set "answer" to "not a right triangle" and just change it if it is, like you do now. Or you can just a boolean. There are many ways to do this.

  7. #7
    TheApostle is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Ok sorry about being stupid there for a second. I understand what you posted now and I implemented it into my code.
    -----------
    if (answer | answer | answer) {

    System.out.print(answer);
    }else{

    System.out.print("Not a right triangle");
    -----------

    Just one problem is that it is saying that it cannot apply the "OR" (|) operator to a string. (the variable "answer" is a string)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Check this ...
    java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

    CJSL

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Read about conditions and booleans also.

    Where you have "answer" must evaluate to either true or false.

Similar Threads

  1. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  2. Triangle Sides program
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 10-06-2008, 03:36 AM
  3. [SOLVED] How do I make a triangle?
    By Zebra in forum Java Applets
    Replies: 6
    Last Post: 05-20-2008, 02:23 PM
  4. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 07:51 PM
  5. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 11:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •