涉及2d阵列的逻辑错误/输出问题

时间:2014-01-13 10:56:21

标签: java arrays logic output multidimensional-array

我并不十分确定我的代码中出现错误的位置(否则我会自己解决)。但是 - 它会对我的输出产生负面影响

enter image description here

我相信这可能是错误的汇编 - 另外,我相当确定我的班级正常运作(我),而其测试人员是错误的来源。该类及其测试器如下所示(为了准确地再现输出及其错误)。这是我期望计算的输出:

enter image description here

非常感谢你的帮助 - 我真的不知所措。

/**
 * This class instantiates Catapult objects with eight private instance variables.
 * It contains one mutator methods to calculates the distance of a projectile fired by the catapult object.
 *
 * Private instance variables include gravity, degreeMeasure, velocity, and distance.
 *
 * @author A. Mackey
 * @version 01/12/14
 */
public class Catapult
{
    //declare private instance variables
    private double gravity = 9.79,                              //gravity affecting the projectile
                   degreeMeasure,                               //degree measurement at which the projectile is fired
                   velocity,                                    //velocity at which the projectile is fired (meters per second)
                   distance;                                    //distance which the projectile travels (in feet)

    //constructor for ojbects of type Catapult
    Catapult(double degMeasure, double velocityValue)
    {
        degreeMeasure = degMeasure;
        velocity = velocityValue / 2.23694;
    }

    /**
     * Mutator method which calculates the distance of a projectile fired by the catapult object (no parameter).
     * @return distance--returns double value for distance of the projectile's travel.
     */
    public double calcDistance()
    {
        return distance = ((Math.pow((velocity), 2) * Math.sin(2 * (Math.toRadians(degreeMeasure))) / gravity)) * 3.28084;
    }
}


/**
 * This class tests the CO2Footprint class.
 * An ArrayList of projectile objects is created to hold the instance variables within the constructor.
 *
 * A for loop is used to use the add() method to add the objects to the ArrayList as they are instantiated.
 * A second for loop is used to call the methods on each object in the ArrayList.
 * A third for loop is used to assign values to the 2d array containing the distance values
 * A fourth for loop is used to print the values of the instance variables for each object as well as other output information.
 *
 * @author A. Mackey
 * @version 01/12/14
 */
import java.util.ArrayList;                                    //import the ArrayList class
public class CatapultTester
{
    public static void main(String[] Args)
    {
        //declare and initialize local variables
        double distance[][] = new double[7][6],                //distance traveled by the projectile
               angle[] = {25, 30, 35, 40, 45, 50},             //angle of projection
               velocity[] = {20, 25, 30, 35, 40, 45, 50};      //velocity of projection
        int counter1 = 0,                                      //counter of first for loop
            counter2 = 0,                                      //counter of third for loop
            counter3 = 0,                                      //counter of fourth for loop
            counter4 = 0,                                      //counter used in fourth for loop for MPH value output
            objectArraylistCounter = 0;                        //counter in third for loop which set values to the distance array

        ArrayList<Catapult> projectile = new ArrayList<Catapult>();
        for(int i = 0; i < 6; i++)
        {
            projectile.add(new Catapult(angle[i], velocity[counter1]));
            if((i % 6) == 0)
            {
                counter1++;
                i = 0;
            }
            if(counter1 == 6)  
            {
                i = 7;
            }
        }

        Catapult dataRecord;                              //creates a new dataRecord object of type ShapesV11

        for(int index = 0; index < projectile.size(); index++)
        {
            dataRecord = projectile.get(index);
            dataRecord.calcDistance();
        }

        for(int i = 0; i < 6; i++)
        {
            dataRecord = projectile.get(objectArraylistCounter);
            distance[counter2][i] = dataRecord.calcDistance();
            if((i % 5) == 0)
            {
                counter2++;
                i = 0;
            }
            if(counter2 == 6)
            {
                i = 6;
            }
        }

        //print output
        System.out.println("                           Projectile Distance (feet)");
        System.out.println("  MPH      25 deg      30 deg      35 deg      40 deg      45 deg      50 deg");
        System.out.print("=================================================================================");
        for(int i = 0; i < 7; i++)
        {
            if((counter4 % 5) == 0)
            {
                System.out.print("\n   " + (int)velocity[(counter4 / 5)]);
            }

            System.out.printf("%12.2f", distance[counter3][i]);
            if((i % 5) == 0)
            {
                counter3++;
                i = 0;
            }
            if(counter3 == 7)
            {
                i = 8;
            }
            counter4++;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在2D阵列上使用计数器很麻烦。我建议你把它们改成嵌套for循环..即

List<Catapult> projectile = new List<Catapult>();
for (int i = 0; i < angle.Length; i++)
{
    for (int j = 0; j < velocity.Length; j++)
    {
        Catapult cata = new Catapult(angle[i], velocity[j]);
        projectile.Add(cata);
        cata.calcDistance();
    }
}

然后,访问您的阵列将是类似的。

//PrintHeader();
int cataCtr = 0;
for (int i = 0; i < angle.Length; i++)
{
    if(i == 0) // PrintNewLineAndAngle();
    for (int j = 0; j < velocity.Length; j++)
    {
        Catapult cata = projectile[cataCtr];
        // PrintCataDistance();
        cataCtr++;
    }
}