The Artima Developer Community
Sponsored Link

Java Answers Forum
Where have I gone wrong?

5 replies on 1 page. Most recent reply: Jul 22, 2005 8:21 AM by John Tyler

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Luke

Posts: 5
Nickname: locoluke
Registered: Jun, 2005

Where have I gone wrong? Posted: Jul 19, 2005 11:05 PM
Reply to this message Reply
Advertisement
No doubt this problem is simple for most; however, I seem to be having difficulty in producing any output. It compiles...just doesn't output anything. Where have I gone wrong, may I ask? Thanks in advance!


public class PythagoreanTriples
{

public static void main( String args[] )
{
int side1;
int side2;
int hypotenuse;
int limit = 5;
int i=0;

/* Loop for side1 to try the values 1-limit. */
for (side1=1; side1<limit; ++side1)
{

/* Loop for side2 to try the values 1-limit. */
for (side2=side1+1; side2<limit; ++side2)
{

/* Loop for hypotenuse to try the values 1-limit */
for(hypotenuse=side2+1; hypotenuse<limit;
++hypotenuse)
{

/* An if statement that determines whether the sum of the two sides squared equals the hypotenuse squared. If this condition is true display side1, side2 and hypotenuse. */

if(side1*side1+side2*side2 == hypotenuse*hypotenuse)
{
System.out.printf(" %d : ( %d, %d, %d )\n", ++i,
side1, side2, hypotenuse);
} // end if
} //end innermost for
} //end inner for
} //end outer for
} //end main

} // end class Triples


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Where have I gone wrong? Posted: Jul 20, 2005 2:22 AM
Reply to this message Reply
Your code is really messy and difficult to read.
Please place your code in pre tags, see the
note that comes up on your left next time you
post.

My immediate guess is that execution never enters
the if statement.

Stich a println statement just before it,
if this comes out, its a simple matter of
logic (your if statement is never satisfied,
hence System.printf never gets executed).

Once again: PLEASE, format your code in a
legible manner (help us help you).

One,
Spike

John Tyler

Posts: 3
Nickname: jmufarrige
Registered: Jan, 2003

Re: Where have I gone wrong? Posted: Jul 21, 2005 1:53 PM
Reply to this message Reply
Your program is performing exactly as written. I'm assuming that you wanted to see "1: (3, 4, 5)" printed out at the end, but the innermost for loop will exit when the hypotenuse == 5 without executing the loop at that point. If you increase your limit to 6 you will see the printout above, if you increase it further to say 100 you will see a nice range of tuples.

This answer was obtained by pasting your code into a class in Eclipse and running through with the debugger a couple of times to find out what was happening.

baidongli

Posts: 2
Nickname: baidongli
Registered: Jul, 2005

Re: Where have I gone wrong? Posted: Jul 21, 2005 11:22 PM
Reply to this message Reply
The method printf(String,int,int,int) is undefined for the type PrintStream !

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Where have I gone wrong? Posted: Jul 21, 2005 11:31 PM
Reply to this message Reply
> The method printf(String,int,int,int) is undefined for the
> type PrintStream !


Good point, maybe he meant to wrap it in the ints in
Integers.

John Tyler

Posts: 3
Nickname: jmufarrige
Registered: Jan, 2003

Re: Where have I gone wrong? Posted: Jul 22, 2005 8:21 AM
Reply to this message Reply
That may be true, although it would result in compiler errors and never would've run in the first place. I was just checking the for/if logic and am running JDK 1.4.2, so I changed the printf to println so it would compile.

Flat View: This topic has 5 replies on 1 page
Topic: help with MenuBar problem Previous Topic   Next Topic Topic: A problem in compiling

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use