Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Why isn't this code working?

    I have this code and when I press the run button only 0's appear instead of random numbers.

    Code:
    /**
     * @(#)chapter17random.java
     *
     * chapter17random Applet application
     *
     * @author
     * @version 1.00 2012/2/14
     */
    
    import java.awt.*;
    import java.applet.*;
    
    public class chapter17random extends Applet {
    
    	TextArea random;
    	int grade[];
    	int counter, random_number;
    
    	public void init() {
    
    		grade=new int[10];
    		random=new TextArea(10,20);
    
    		for (int counter=0;counter<0;counter=counter+1) {
    
    			random_number=(int)(100*Math.random());
    			grade[counter]=random_number;
    
    		}
    
    		random.append(""+grade[0]+"\n");
    		random.append(""+grade[1]+"\n");
    		random.append(""+grade[2]+"\n");
    		random.append(""+grade[3]+"\n");
    		random.append(""+grade[4]+"\n");
    		random.append(""+grade[5]+"\n");
    		random.append(""+grade[6]+"\n");
    		random.append(""+grade[7]+"\n");
    		random.append(""+grade[8]+"\n");
    		random.append(""+grade[9]+"\n");
    
    		add(random);
    
    	}
    
    }

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    you never set counter and there are problems with your for loop- I threw this in debugger and it worked fine...
    Code:
    int[] myGrade = new int[10];
    int randomNum;
    
    for(int i=0; i<myGrade.length; i++)
    {
      randomNum = (int)(100*Math.random());
      // I inserted a break point in preceeding line and watched values come through fine
    }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    Funnyjoe66 (02-21-2012)


  •  

    Posting Permissions

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