Results 1 to 6 of 6
  1. #1
    Actaeonis is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default 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);
    
    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Very basic problem with a for loop.

    Quote Originally Posted by Actaeonis View Post
    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!

    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!):

    Java 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?

    Java 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!

  3. #3
    Actaeonis is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    Milanek is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Very basic problem with a for loop.

    Hi,

    remove this from the for loop:
    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);
    This is what you want:

    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);
     
    
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Very basic problem with a for loop.

    Quote Originally Posted by Milanek View Post
    This is what you want:

    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++ ;
        }....
    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.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  2. Very basic java problem
    By mrparker2 in forum New To Java
    Replies: 6
    Last Post: 05-26-2011, 09:50 PM
  3. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 08:45 PM
  4. Basic help here please....for each loop
    By selam in forum New To Java
    Replies: 9
    Last Post: 12-28-2008, 04:44 PM
  5. [SOLVED] very basic java problem
    By sales1 in forum New To Java
    Replies: 13
    Last Post: 08-20-2008, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •