...

View Full Version : Why isn't this code working?



pineappelle
02-15-2012, 01:27 AM
I have this code and when I press the run button only 0's appear instead of random numbers.


/**
* @(#)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);

}

}

alykins
02-15-2012, 04:02 PM
you never set counter and there are problems with your for loop- I threw this in debugger and it worked fine...


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
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum