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.
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.
Quote:
Originally Posted by
Actaeonis
Hey, first of all, I'm a total newbie in java but I have this question,
Welcome to Java, and welcome to the Java-Forums.org!
Quote:
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.
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?
Regarding some of your code (and thanks for using code tags in your first post!):
Code:
public class oef6_1 extends Applet {
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?
Code:
public void paint (Graphics g) {
for ( int t = 1; t < 11 ; t++) {
thing = Math.random();
if (thing >= 0.5) {
number++ ;
}
else {
numberr++ ;
}
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.
Best of luck, and again welcome!
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.
Re: Very basic problem with a for loop.
Hi,
remove this from the for loop:
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);
This is what you want:
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.
Quote:
Originally Posted by
Milanek
This is what you want:
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++ ;
}....
Why are you advising him to still do the for loop inside of the paint method? Do you realize that every time someone resizes the applet the for loop will be repeated and the calculation results changed? I'm sure that this is not the intended behavior.