Results 1 to 4 of 4
Thread: Loop help!
 09212013, 12:46 AM #1Member
 Join Date
 Sep 2013
 Posts
 4
 Rep Power
 0
Loop help!
Hi, I'm quite new to programming and Java. I need some help with my homework. The problem is in the image below:
And here is my code so far:
Java Code:// Duy Nguyen (ID:02104267) // Duy Nguyen (ID:02104267) public class Points { public static void main( String[] args ){ // Input check if( args.length != 7 ){ System.err.println( "ERROR: invalid number of command line " + "inputs." ); System.err.println( "Usage: points X1 Y1 X2 Y2 X3 Y3 N" ); System.exit( 1 );} // Command line input double x1= Double.parseDouble(args[0]); double y1= Double.parseDouble(args[1]); double x2= Double.parseDouble(args[2]); double y2= Double.parseDouble(args[3]); double x3= Double.parseDouble(args[4]); double y3= Double.parseDouble(args[5]); double n= Double.parseDouble(args[6]); // Distance+ line double distance= Math.sqrt(Math.pow(x1 x2, 2) + Math.pow(y1 y2, 2)); double slope= (y2y1)/(x2x1); // Points int i= 1; System.out.printf("The distance from(%.2f, %.2f) to (%.2f, %.2f) is: %f ", x1, y1, x2, y2, distance); double xi, yi; for(i=1; i<= n; i++) { xi= (x2/n)+ x1; yi= (y2/n)+ y1; System.out.printf("%.2f, %.2f", xi, yi) } } } }
Last edited by huuduy216; 09212013 at 12:59 AM.
 09232013, 08:51 AM #2Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 11
Re: Loop help!
You see that float you're using to display the %f placeholder value? Declare another variable to store the smallest of those, as well as another variable to store the index of the args array corresponding to that value, then simply use that index variable to display the point directly out of the args array.
 09232013, 11:36 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
Re: Loop help!
Also, don't try to calculate the slope; it isn't defined if P1 and P2 are the endpoints of a vertical line segment (x2 == x1). Note that if you walk over the points of the line, if the distance of a point is larger than the distance of the previous point, the previous point has the minimal distance to that fixed third point.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 09232013, 03:43 PM #4Member
 Join Date
 Sep 2013
 Posts
 4
 Rep Power
 0
Re: Loop help!
This is what I came up with.
Java Code:// Duy Nguyen (ID:02104267) // Duy Nguyen (ID:02104267) public class Points { public static void main( String[] args ){ // Input check if( args.length != 7 ){ System.err.println( "ERROR: invalid number of command line " + "inputs." ); System.err.println( "Usage: points X1 Y1 X2 Y2 X3 Y3 N" ); System.exit( 1 );} // Command line input double x1= Double.parseDouble(args[0]); double y1= Double.parseDouble(args[1]); double x2= Double.parseDouble(args[2]); double y2= Double.parseDouble(args[3]); double x3= Double.parseDouble(args[4]); double y3= Double.parseDouble(args[5]); double n= Double.parseDouble(args[6]); // Distance+ line double distance= Math.sqrt(Math.pow(x1 x2, 2) + Math.pow(y1 y2, 2)); // Points int i1= 1; int i2= 1; System.out.printf("The distance from(%.2f, %.2f) to (%.2f, %.2f) is: %f \n", x1, y1, x2, y2, distance); double xi= x1; double yi= y1; double closestDistance= Math.sqrt(Math.pow(xi x3, 2) + Math.pow(yi y3, 2)); int closesti= 0; //hold the number i2 that has shortest distance for(i1=1; i1<=n;i1++) { if (i1== closesti) { System.out.printf("(%.2f, %.2f) < Closest point to (%.2f,%.2f)\n", xi, yi, x3, y3); } else { System.out.printf("(%.2f, %.2f)\n", xi, yi); } for(i2=1; i2<= n; i2++) { xi+= (x2x1)/10; yi+= (y2y1)/10; if(Math.sqrt(Math.pow(xi x3, 2) + Math.pow(yi y3, 2)) <= closestDistance) { closestDistance= Math.sqrt(Math.pow(xi x3, 2) + Math.pow(yi y3, 2)); closesti= i2; } } } } }
Similar Threads

Converting a while loop to a for loop
By danboy4 in forum New To JavaReplies: 7Last Post: 08082012, 03:35 PM 
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11072011, 03:25 AM 
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10012011, 02:12 AM 
JTextField loop 2x forloop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02162011, 06:46 PM
Bookmarks