简体   繁体   中英

Storing time intervals in a java array

This is a homework assignment I am working on. Before I ask, I am not looking for a piece of code. I would just like some assistance figuring out where I may have gone wrong.

The objective of the assignment is to run an applet that loops 100,000 times. There are 25 intervals and the time at each interval needs to be stored in an array. So far I am able to get the applet to run and print correctly, but the array is only storing the first value. I'm thinking that there is a formatting issue or something small keeping me from figuring out this problem, but I've searched for a while now and I can't seem to figure this out.

The code I have is as follows:

    public class Line1TimedTest extends JApplet 
    {
        long start, elapsed, arrayValue;
        int x1=50, y1=50, x2=500, y2=500, limit=100000, value;
        DecimalFormat fmt;


        public void init() 
        {
    start = System.nanoTime();
    fmt = new DecimalFormat("###,###,###,###,###");
    }



    public void paint (Graphics g) 
    {
        int x, y, temp;
        float m, b;
            long [] arrayTimes = new long [25];

        for (int i = 1; i <=limit; i++) 
        {   
            m = (float)(y2-y1)/(x2-x1);
            b = y1 - m * x1;
            int value = 0;

            for (x = x1; x <= x2; x++)
            {
                y = (int)(m * x + b + 0.5);
                g.drawRect(x,y,0,0);

            }
            if (i%4000==0)
            {
                elapsed = System.nanoTime() - start;
                arrayValue = elapsed;
                arrayTimes[value] = arrayValue;
                System.out.println(fmt.format(i) + " lines drawn");
                value++;
            }


        }


    //insert print statement


     } 

}

The program compiles and runs with no errors, but the array is only being populated with a value at index 0. All other values are 0. If anyone could take a look and see what I might be missing I'd really appreciate it.

Thanks

In each loop you overwrit the integer value with a new one.

arrayTimes[i%4000] = arrayValue;

should work.

Alternatively define the integer value outside the loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM