尽管语句为

时间:2017-09-28 02:13:27

标签: java

public class Diabetes {

  public static void main(String[] args) {
    double T = Double.parseDouble(args[0]);
    double deltat = Double.parseDouble(args[1]);
    int n = StdIn.readInt();
    int i = 1;
    double r = StdIn.readDouble();
    double[] positionX = new double[n];
    double[] positionY = new double[n];
    double[] velocityX = new double[n];
    double[] velocityY = new double[n];
    double[] mass = new double[n];
    String[] image = new String[n];


    double[] deltaX = new double[n * n];
    double[] deltaY = new double[n * n];
    double[] radius = new double[n * n];
    double[] force = new double[n * n * n];
    double g = 6.67E-11;
    double[] forceX = new double[n * n];
    double[] forceY = new double[n * n];               
    double[] cosine = new double[n * n];
    double[] sine = new double[n * n];
    double[] accelerationX = new double[n * n];
    double[] accelerationY = new double[n * n];
    double[] newvelocityX = new double[n * n];
    double[] newvelocityY = new double[n * n];
    double[] newpositionX = new double[n * n];
    double[] newpositionY = new double[n * n];

    int positionX_counter = 0;
    int positionY_counter = 0;
    int velocityX_counter = 0;
    int velocityY_counter = 0;
    int mass_counter = 0;
    int image_counter=0;
    //System.out.println("n = " + n + "r = " + r);

        for (int j = 0; j < n; j++) {
            for(int k = 1; k < 7; k++)
            if (k == 6) {
            image[image_counter] = StdIn.readString();
            System.out.println(image[image_counter]);
            image_counter++;
            //System.out.println("image counter = " + image_counter);
            }
            else if (k == 5) {
            mass[mass_counter] = Double.parseDouble(StdIn.readString());
            System.out.println(mass[mass_counter]);
            mass_counter++;
            //System.out.println("masscounter = " + mass_counter);
            }
            else if (k == 4) {
            velocityY[velocityY_counter] = Double.parseDouble(StdIn.readString());
            System.out.println(velocityY[velocityY_counter]);
            velocityY_counter++;
            //System.out.println("velcounter = " + velocity_counter);
            }
            else if (k == 3) {
            velocityX[velocityX_counter] = Double.parseDouble(StdIn.readString());
            System.out.println(velocityX[velocityX_counter]);
            velocityX_counter++;
            //System.out.println("velcounter = " + velocity_counter);
            }
            else if (k == 2) {
            positionY[positionY_counter] = Double.parseDouble(StdIn.readString());
            System.out.println(positionY[positionY_counter]);
            positionY_counter++;
            }
            else if (k == 1) {
            positionX[positionX_counter] = Double.parseDouble(StdIn.readString());
            System.out.println(positionX[positionX_counter]);
            positionX_counter++;
            }
        }
        for (int m = 0; m <= 10; m++) {
            if ( i == 5 ) { 
                i = 1;
            }
            else {
                    deltaX[m] = positionX[i] - positionX[m];
                    deltaY[m] = positionY[i] - positionY[m];
                    radius[m] = Math.sqrt((deltaX[i] * deltaX[i]) + (deltaY[i] * deltaY[i]));
                    force[m] = (g * mass[m] * mass[i]) / (radius[m] * radius[m]);
                    cosine[m] = (deltaX[m] / radius[m]);
                    sine[m] = (deltaY[m] / radius[m]);
                    forceX[m] = force[m] * cosine[m];
                    forceY[m] = force[m] * sine[m];
                    accelerationX[m] = forceX[m] / mass[i];
                    accelerationY[m] = forceY[m] / mass[i];
                    newvelocityX[m] = velocityX[i] + (deltat*accelerationX[m]);
                    newvelocityY[m] = velocityY[i] + (deltat*accelerationY[m]);
                    newpositionX[m] = positionX[i] + (deltat*velocityX[i]);
                    //System.out.println(newpositionX[m]);
                    newpositionY[m] = positionY[i] + (deltat*velocityY[i]);
                    i++;
                    System.out.println(i);
                }
            }
        }
  }      
嘿,伙计们,我已经写过的代码真的很困惑,以及它为什么会抛出错误。基本上我有一些长度数组(5),我试图用它们进行一些计算并将它们存储在一个长度为10的数组中。所以代码设计用五个数组进行十次计算,所以很自然我设计它在for循环中有两个不同的计数器。第一个应该在它达到5时重置,而另一个在它达到10时杀死循环。没有任何意义的是程序正在吐出输出i = 5,但它没有进入if语句并重置自身,所以我得到一个关于我的数组超出范围的错误。

感谢您的帮助

0 个答案:

没有答案