Results 1 to 2 of 2
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default why does the program keep running?

    Java Code:
    package testtriangletype;
    
    
    
    import java.util.Random;
    
    
    public class TestTriangleType {   
        private int max = 3, eqY = 0, eqX = 0, 
                      chgX = 0, chgY = 0, triangles = 5, i;  
        private double length1, length2, length3, y1, x1, y2, x2, y3, x3;
    
        public TestTriangleType(){ 
            Random rand = new Random(12);
            for (i = 0; i < triangles; i++)  
            {   do {x1 = rand.nextInt(max);
                    x2 = rand.nextInt(max);
                    y1 = rand.nextInt(max);
                    y2 = rand.nextInt(max);
                }while ((x1 == x2) && (y1 == y2)); 
                if (x1 == x2)
                    eqX = 1;      
                if (y1 == y2)
                    eqY = 1;    
                while (eqX != 0)
                {   x3 = rand.nextInt(max);
                if (x3 != x1)
                    eqX = 0;
                    chgX = 1;
                }
                while (eqY != 0)
                {   y3 = rand.nextInt(max);
                if (y3 != y1)
                    eqY = 0;
                    chgY = 1;
                } 
                while(true)
                {   if (chgX == 0)
                    x3 = rand.nextInt(max);
                if (chgY == 0)
                    y3 = rand.nextInt(max);
                if ((x1 == x3) || (y2 == y3)){}
                else {break;}     
                } 
                System.out.println("Triangle " + (i + 1) + "'s cordinates followed "
                                  + "by the side lenghts are:");
                length1 = Math.sqrt(Math.pow((y2 - y1),2) + Math.pow((x2 - x1),2));
                length2 = Math.sqrt(Math.pow((y3 - y2),2) + Math.pow((x3 - x2),2));
                length3 = Math.sqrt(Math.pow((y1 - y3),2) + Math.pow((x1 - x3),2));
                System.out.printf("(" + x1 + ", " + y1 + ")" + "(" + x2 + ", " + y2
                                  + ")" + "(" + x3 + ", " + y3 + ")");
                System.out.printf("|(%.2f, %.2f, %.2f)",length1, length2, length3);
                System.out.println();
                if ((length1 != length2) && (length1 != length3) && length2 !=
                     length3)
                    System.out.println(" SC");
                else if (length1 == length2 && length1 == length3)
                    System.out.println(" EQ");
                else
                    System.out.println(" IS");
                
                }
        }
        
        public static void main(String[] args) {
            new TestTriangleType();
        }
    }
    If I put max = 3 the program does not finish but if max = 150 it does; Why?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: why does the program keep running?

    Try debugging the code by adding some println statements that print out the values of variables as they are changed and used. Make sure you put a println inside each loop that could run forever that shows the variables that controls how the loop can end.
    The print out will show you what the computer sees and help you understand what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Program running in eclipse but JAR won't run?
    By Jma84 in forum New To Java
    Replies: 4
    Last Post: 08-13-2012, 11:52 AM
  2. Not getting why the program is not running
    By jnjh in forum New To Java
    Replies: 5
    Last Post: 04-07-2011, 10:25 AM
  3. Help with running the program :)
    By dj_ee3 in forum New To Java
    Replies: 2
    Last Post: 02-27-2011, 11:19 PM
  4. Running Java program
    By Natalia in forum New To Java
    Replies: 5
    Last Post: 11-27-2010, 09:52 PM
  5. my program is running bit slow!!
    By Arn00p in forum AWT / Swing
    Replies: 18
    Last Post: 05-05-2010, 04:14 PM

Posting Permissions

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