my for (int i =0; i < max; i++) loop wont count up but all the other variables in do
OK...
thanks for stopping to read :)
this code is starting to annoy the crap out of me aha
my issue is that the i in the for loop wont count up :(
any one know why? please help
**BTW i am a beginerish programmer**
its this for loop:
Code :
for (int i = 0; i < max; i=i+1) {
//m =store[i];
while (j<10){
g2.drawRect((i*SIZE), (LINE-((j+1)*SIZE)), SIZE, SIZE);
j++;
g2.drawString("Number of students = " + i , 20, 20);
}
}
this is the rest of the code for this class:
Code :
import java.awt.*;
import javax.swing.*;
public class BellCurve extends JComponent {
public static final int SIZE = 10;
public static final int LINE = SIZE*(62-10);
private String [] values;
Score s;
public int students;
public int k = 0;
private int f = 0;
private int j = 0;
private int l;
private int m;
private int max;
private int [] scores;
private int [] store;
public BellCurve(String [] values) {
this.values = values;
s = new Score(values);
max = Integer.parseInt(values[2]);
store = new int[max];
}
public BellCurve() {
this.values = new String [5];
s = new Score(values);
max = 100;
store = new int[max];
}
public void paintComponent(Graphics g) {
i = 0;
j = 0;
Graphics2D g2 = (Graphics2D) g.create();
//students = s.getStudents();
//scores = s.getScore();
g2.drawLine(0, LINE, 1020, LINE);
g2.drawString("Range of marks = " + s.getRMin() + " - " + s.getRMax(), 20, LINE+35);
g2.drawString("Mean and Standard Deviation = " + s.getMu() + ", " + s.getSigma(), 20, LINE+50);
if (k<students){
g2.drawString("Number of students = " + k, 20, LINE+20);
//l=scores[k];
// store[l]++;
for (int i = 0; i < max; i=i+1) {
//m =store[i];
while (j<10){
g2.drawRect((i*SIZE), (LINE-((j+1)*SIZE)), SIZE, SIZE);
j++;
g2.drawString("Number of students = " + i , 20, 20);
}
}
k+=1;
}else{
}
}
}
Re: my for (int i =0; i < max; i++) loop wont count up but all the other variables in
Quote:
the i in the for loop wont count up
What do you see when you execute the code?
How do you know what the the value of i is? Add a println statement inside the for statement that prints the value of i so you can see its value as the loop executes.
Re: my for (int i =0; i < max; i++) loop wont count up but all the other variables in
Hi,
According to your code, in method paintComponent() you have some errors: i is used just in the second line without a type and then you try to declare i again in the loop. The code should not compile at all.
The problem is that the loop is not working or the code is not compile?
But, for debuggind, you can try to put a System.out.println(String.valueOf(max)) just before
for (int i = 0; i < max; i=i+1) {
and check if the max is zero (if it's zero then the loop won't execute because the condition to loop is not satisfied).
Chris