Results 1 to 9 of 9
- 10-07-2008, 06:24 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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!!
- 10-07-2008, 07:06 PM #2
Java Code:if (A | B | C) { true; }else{ false; }
- 10-07-2008, 07:08 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
Keeps saying that line 29 (true;) and line 31 (false;) are not statements : \
- 10-07-2008, 07:09 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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.
- 10-07-2008, 07:15 PM #5
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.
- 10-07-2008, 07:17 PM #6
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Norm gave you one way to solve this. Read up on how if-else works. I'd do it like this:
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.Java Code:if (something) { // code } else if (something else) { // code } else { // code }
- 10-07-2008, 07:19 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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)
- 10-07-2008, 07:20 PM #8
Check this ...
java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html
CJSL
- 10-07-2008, 08:39 PM #9
Similar Threads
-
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01-10-2009, 02:08 PM -
Triangle Sides program
By jamesov89 in forum New To JavaReplies: 6Last Post: 10-06-2008, 03:36 AM -
[SOLVED] How do I make a triangle?
By Zebra in forum Java AppletsReplies: 6Last Post: 05-20-2008, 02:23 PM -
Pascal Triangle help
By Magic101 in forum New To JavaReplies: 4Last Post: 05-01-2008, 07:51 PM -
Making triangle
By banie in forum New To JavaReplies: 4Last Post: 02-02-2008, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks