Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2012, 01:27 AM   PM User | #1
pineappelle
New Coder

 
Join Date: Jun 2011
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
pineappelle is an unknown quantity at this point
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);

	}

}
pineappelle is offline   Reply With Quote
Old 02-15-2012, 04:02 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Users who have thanked alykins for this post:
Funnyjoe66 (02-21-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 PM.


Advertisement
Log in to turn off these ads.