Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with FOR loop..Need Help ASAP...

 
ELL TIM
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've this class assignment that says:

Design and write an ArithmeticGUI class that allows users to input play with numbers multiplication. The GUI should contain three JTextField, one for input first number, second for second number and third for the result of multiplication of both numbers. There should be one button which the user can signal that correct multiplication result has been entered. The multiplication table of the second number should be displayed in the text area and the correctness of the multiplication result should also be displayed in a text field, but this should be set uneditable. A sample Arithmetic GUI interface is displayed below:

If users input a correct multiplication result of two numbers, the below should happened:



If users input a wrong multiplication result of two numbers, the below should happened:




i've already try it but i have problem at the for loop structure...
here the java code that i try..





please help me solve this...
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
 
K. Tsang
Bartender
Posts: 3398
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you really doing is a times table. Then the Times assignment of the Cattle Drive may be some help.

If you figure out the algorithm for that, you should have solve your original problem.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button; it makes your code much easier to read.

You are right about having problems with for loops; I suggest you find a book and read up about for loops, or find the syntax in the Java Tutorials.

Anmd welcome to JavaRanch
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (num1 * num2 == result) //(have problem)

I'm not sure what you are trying to do here. What you have in your parens: "num1 * num2 == result" is essentially a boolean. it's either 'true' or 'false'.

A 'for' statement doesn't take a boolean - it takes three things for setting up, testing, and updating variables. Then, the code inside the curly brackets potentially runs multiple times.

So really, the question is, what are you trying to DO there? Are you trying to test something? i don't think so, because it's a repeat of what's in your if statement just above it.

I assume you are trying to loop around and print out all those products.

So, as others have said, look in a book or online tutorial for how to set up a for-loop. Then, see if you can write one that does nothing more that prints "hello" 12 times. Then get it to print something like:

1
2
3
4
5
6
7
8
9
10
11
12

etc, and add a little bit each time.
 
ELL TIM
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
for (num1 * num2 == result) //(have problem)

I'm not sure what you are trying to do here. What you have in your parens: "num1 * num2 == result" is essentially a boolean. it's either 'true' or 'false'.

A 'for' statement doesn't take a boolean - it takes three things for setting up, testing, and updating variables. Then, the code inside the curly brackets potentially runs multiple times.

So really, the question is, what are you trying to DO there? Are you trying to test something? i don't think so, because it's a repeat of what's in your if statement just above it.

I assume you are trying to loop around and print out all those products.

So, as others have said, look in a book or online tutorial for how to set up a for-loop. Then, see if you can write one that does nothing more that prints "hello" 12 times. Then get it to print something like:

1
2
3
4
5
6
7
8
9
10
11
12

etc, and add a little bit each time.



Sorry bout the code,i'm new to this site..also new to java programming...the thing is i want the output to print out like this when user key in the second number,then the multiplication are base on the second number.

1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
...
...
...
12 x 2 = 24

i try using this code but still cant get what i want..

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one of the hardest things to learn as a new programmer is to not try and do too much at once. The less you try and accomplish on each build/debug/test cycle, the easier it is to find your problems.

So, I'd suggest this:

try and write code that when the user enters their second number, you get it to print the number 1-12, each on a new line. Get that to work. ONLY after you are sure that is working perfectly, try and change it to print

1 x <the input second number>
2 x <the input second number>
... etc

ONLY after you have that working, try and get it to print the actual product on the end.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ELL TIM wrote:..also new to java programming...
Has whoever is teaching you not taught you about the structure of a for loop?
 
ELL TIM
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just figured it out actually. Thanks for the tips! (^_^)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(OT) Mostly I was just entertained that we got to say "buttCheck".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic