Results 1 to 3 of 3
Thread: I need help with this program
- 07-04-2007, 05:09 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
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.
ThanksJava 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]); } } } } }
Daniel:o
- 07-04-2007, 05:12 AM #2
Senior Member
- Join Date
- Jun 2007
- Posts
- 111
- Rep Power
- 0
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
- 07-04-2007, 05:14 AM #3
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
cannot run the program
By amiey in forum New To JavaReplies: 1Last Post: 11-20-2007, 04:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
Why does this program not end?
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks