Question:

Basic Java triangle program help

by  |  earlier

0 LIKES UnLike

My program is designed to accept 3 lengths (inputed directly into the program) and display whether the triangle is equilateral, isosceles, or scalene. When I run it, all of the outputs are displayed. Could someone tell me what's wrong? (I'm using JCreator)

import java.io.*;

public class Triangles

{

public static void main(String [] args)

{

int num1;

int num2;

int num3;

num1 = 3;

num2 = 3;

num3 = 3;

if ((num1 == num2) && (num1 == num3))

{

System.out.println("Your triangle is equalateral, all sides are the same length!");

}

if (((num1 == num2) && (num1 != num3)) || ((num1 == num3) && (num1 != num2)) || ((num2 == num3) && (num2 != num1)));

{

System.out.println("Your triangle is isosceles, two sides are the same length!");

}

if ((num1 != num2) && (num1 != num3) && (num2 != num3));

{

System.out.println ("Your triangle is scalene, no sides are the same length!");

}

if ((((num1 num2)<num3) || ((num2 num3)<num1) || ((num1 num3)<num2)));

{

System.out.println ("Your 'triangle' isn't a triangle!");

}

}

}

 Tags:

   Report

4 ANSWERS


  1. On the message output ifs, you have a ; at the end of each line.  This is a statement terminator, and so the compiler takes this to be the end of each if statement, so that section of the program runs as follows:-

    the statement

    if (((num1 == num2) &amp;&amp; (num1 != num3)) || ((num1 == num3) &amp;&amp; (num1 != num2)) || ((num2 == num3) &amp;&amp; (num2 != num1)))

    is evaluated and then nothing is done as the following

    ;

    ends the statement.  Then the

    System.out.println(&quot;Your triangle is isosceles, two sides are the same length!&quot;);

    }

    is executed - you don&#039;t need braces after an if statement if you only want to execute a single command, but they are good practice.

    Next it goes onto the next if, evaluates it, does nothing, because the ; again ends the statement block, and it prints the next output.  Remove those ; at the end of the if lines and it should work fine.

    On another point, it is better programming practice to do the first output if, then make the next if an &quot;else if&quot; - the 3 ifs are mutually exclusive (only one of them will ever be true).  In your program&#039;s current state, each one will always be evaluated, if you make the 2nd 2 &quot;else&quot;s, then the program will skip them once one if found to be true and will thus be more efficient (make sure you get your braces {} correct if you do this).  You may think that this doesn&#039;t matter, but it is a good habit to get into because in a bigger program with repeating iterations and many if statements it can make a big difference to the execution speed.


  2. This line has no opperators:

    if ((((num1 num2)&lt;num3) || ((num2 num3)&lt;num1) || ((num1 num3)&lt;num2)));

    Also, if statments are not meant to have ; at the end of the first line! it should be like this

    if (something){

    //code block here executed if &quot;something&quot; is true

    }


  3. You shouldn&#039;t put ; after the end of if.

    Regards

    http://www.ITSupportCell.com


  4. Read over it like 20 times, can&#039;t find the problem, suggest trying else if statments after the first if and an else statment in last if statement...help this hopes.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.