Results 1 to 6 of 6
- 11-18-2011, 06:15 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Very basic problem with a for loop.
Hey, first of all, I'm a total newbie in java but I have this question,
I want to make a program that checks wether a number is greater or equal to 0.5 or less than 0.5 with the math.random function. I think I got it working but it prints all the numbers on the same spot.
Java Code:// Checking if a number is either; less or more than 0.5 import java.applet.*; import java.awt.*; public class oef6_1 extends Applet { double thing; int number = 0; int numberr = 0; // where number is >= 0.5, and numberr < 0.5. public void paint (Graphics g) { for ( int t = 1; t < 11 ; t++) { thing = Math.random(); if (thing >= 0.5) { number++ ; } else { numberr++ ; } g.drawString (("There are" +' '+ Integer.toString(number) +' '+ "numbers bigger or equal to 0.5"),10 ,10); g.drawString (("There are" +' '+ Integer.toString(numberr) +' '+ "numbers less than 0.5"),10 ,45); } } }
-
Re: Very basic problem with a for loop.
Welcome to Java, and welcome to the Java-Forums.org!
Can you elaborate? What do you mean "it prints all the numbers on the same spot", and where exactly and what exactly do you want displayed?I want to make a program that checks wether a number is greater or equal to 0.5 or less than 0.5 with the math.random function. I think I got it working but it prints all the numbers on the same spot.
Regarding some of your code (and thanks for using code tags in your first post!):
I see that you're using Applet which is an AWT component. AWT is a very old GUI library and has been mostly superseded by Swing. So why not instead use a JApplet and Swing?Java Code:public class oef6_1 extends Applet {
Here you're doing program logic within a paint method (which if using Swing the counterpart would be the paintComponent method of a JComponent), and this is something that you don't want to do since paint can be called multiple times and almost all out of your control. Better to call this in the applet's init() method and just display the results in the paint method.Java Code:public void paint (Graphics g) { for ( int t = 1; t < 11 ; t++) { thing = Math.random(); if (thing >= 0.5) { number++ ; } else { numberr++ ; }
Best of luck, and again welcome!
- 11-18-2011, 06:54 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: Very basic problem with a for loop.
Hey, thanks for your anwser ^^.
Well concerning the awt and Paint, this is the way it's being teached at my school, I have no idea of how to do it otherwise.
The goal of the program is to get the following;
"There are 5 numbers greater or equal to 0.5"
"There are 5 numbers less than 0.5"
Whilst now, it compresses the 1-2-3-4-5 on the same spot instead of just saying 5.
-
Re: Very basic problem with a for loop.
Your problem may be due to your not calling the super's paint method. I suggest the following:
1) Do your for loop and all in the applet's init method.
2) Call repaint after the for loop has been completed.
3) Display the results in the paint method
4) But the first method call in paint should be super.paint(g) to allow the applet to delete any previous painting.
- 11-18-2011, 07:15 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Re: Very basic problem with a for loop.
Hi,
remove this from the for loop:
This is what you want:Java Code:g.drawString (("There are" +' '+ Integer.toString(number) +' '+ "numbers bigger or equal to 0.5"),10 ,10); g.drawString (("There are" +' '+ Integer.toString(numberr) +' '+ "numbers less than 0.5"),10 ,45);
Java Code:// Checking if a number is either; less or more than 0.5 import java.applet.*; import java.awt.*; public class oef6_1 extends Applet { double thing; int number = 0; int numberr = 0; // where number is >= 0.5, and numberr < 0.5. public void paint (Graphics g) { for ( int t = 1; t < 11 ; t++) { thing = Math.random(); if (thing >= 0.5) { number++ ; } else { numberr++ ; } } g.drawString (("There are" +' '+ Integer.toString(number) +' '+ "numbers bigger or equal to 0.5"),10 ,10); g.drawString (("There are" +' '+ Integer.toString(numberr) +' '+ "numbers less than 0.5"),10 ,45); } }
-
Re: Very basic problem with a for loop.
Similar Threads
-
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Very basic java problem
By mrparker2 in forum New To JavaReplies: 6Last Post: 05-26-2011, 08:50 PM -
What am I doing wrong in this basic problem
By Blaedel in forum New To JavaReplies: 10Last Post: 10-11-2009, 07:45 PM -
Basic help here please....for each loop
By selam in forum New To JavaReplies: 9Last Post: 12-28-2008, 03:44 PM -
[SOLVED] very basic java problem
By sales1 in forum New To JavaReplies: 13Last Post: 08-20-2008, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks