Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 07:09 AM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
I need help with this program
It doesn't show me any error but it doesn't generate enough questions... i'm new at java so i really dont know how to get this to work... the program is for generating questions on subtraction of fractions.. i need an eye to help me on this.
Code:
public class FractionSubtraction { public static void main(String[] args) { int a, b, c, e, f, g, ans, deno; String sent, num1, num2, fin; int count = 20; //number of questions String[] list = new String[count + 1]; for (int i=0; i < list.length; i++) { a = (int) (1 + Math.random() * 20); b = (int) (1 + Math.random() * 20); c = (int) (1 + Math.random() * 20); e = (int) (Math.random() * 11); f = (int) (Math.random() * 11); while (b == 1) { b = (int) (1 + Math.random() * 20); } while (e < f) { e = (int) (Math.random() * 11); f = (int) (Math.random() * 11); } while (a >= b) { a = (int) (1 + Math.random() * 20); } while (c >= b || (c == a && b != 2)) { c = (int) (1 + Math.random() * 20); } while ((e*b + a) - (f*b + c) <= 0) { while (e < f) { e = (int) (Math.random() * 11); f = (int) (Math.random() * 11); } while (a >= b) { a = (int) (1 + Math.random() * 20); } while (c >= b || (c == a && b != 2)) { c = (int) (1 + Math.random() * 20); } } g = 0; ans = (e*b + a) - (f*b + c); while (ans >= b) { ans = ans - b; g++; } deno = b; if (e > 0) { num1 = "" + e + " "; } else { num1 = ""; } if (f > 0) { num2 = "" + f + " "; } else { num2 = ""; } if (g > 0) { fin = "" + g + " "; } else { fin = ""; } if (ans > 0) //reduce fraction in final answer { for (int red = 2; red <= deno; red++) { while (ans%red == 0 && deno%red == 0) { ans = ans/red; deno = deno/red; } } sent = "" + num1 + a + "/" + b + " - " + num2 + c + "/" + b + " = " + fin + ans + "/" + deno; } else { sent = "" + num1 + a + "/" + b + " - " + num2 + c + "/" + b + " = " + fin; } for (int j = 0; j < i; j++) { if (sent.equals(list[j])) // eliminate any repeated questions { i--; break; } else if (j == i -1) { list[i] = sent; System.out.println(list[i]); } } } } }
Thanks

Daniel
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 07:12 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
You can debug this by simply putting some print statements in your loops and print out the variables involved with producing your output That way you can see what happens up to the point where it gets stuck.

I would venture to guess you are either getting stuck in an infinite loop or your loop is ending prematurely.
In either case you will be able to figure that out by printing out the variables involved.
In order to learn how to program you must also learn common debugging techniques. If you cannot use a debugging tool then print statements are you next best friend in debugging.

Greetings

Eric
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 07:14 AM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
Sorry i counted accordin to my program but not the one i posted here the infinite while loop i mentioned is the following:

Code:
while ((e*b + a) - (f*b + c) <= 0)
Thanks

Daniel
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 10:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 04:40 PM
cannot run the program amiey New To Java 1 11-20-2007 06:13 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 11:33 PM
Why does this program not end? trill New To Java 1 08-07-2007 09:22 AM


All times are GMT +3. The time now is 04:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org