【问题标题】:Logic Error/Output Issue Involving 2d-Array涉及二维阵列的逻辑错误/输出问题
【发布时间】:2014-01-13 10:56:20
【问题描述】:

我不太确定错误发生在我的代码中的哪个位置(否则我会自己修复它)。但是——它会对我的输出产生负面影响

我相信这可能是错误的汇编——此外,我相当确定我的课程运行正常(我认为),而它的测试人员是错误的来源。下面显示了类及其测试器(为了准确地重现输出及其错误)。这是我期望计算的输出:

非常感谢您的帮助-我真的很茫然。

/**
 * 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++;
        }
    }
}

【问题讨论】:

标签: java arrays logic output multidimensional-array


【解决方案1】:

在二维数组上使用计数器很麻烦。我建议您将它们更改为嵌套的 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++;
    }
}

【讨论】:

  • 非常感谢您对嵌套循环的建议——它确实美化了我的代码。我绝对应该想到这个——明天学期结束前强调,这是我期末前的最后提交。使用这个新代码,这一切都很好:pastebin.com/siV6Cgps——但是,输出仍然很不稳定。它只是为我的第一个距离打印一个真实值——我不太确定我在新测试仪中的错误是什么。它看起来像这样:i.imgur.com/bfBVjTf.png 任何线索这里可能有什么问题?
  • 另外——我很乐意为你的评论点赞并给你代表或其他任何东西——但我在这里太新了,不能这样做,除此之外,我的代表从我上次的时候就被彻底炸毁了题。由于我的评论,一群高级代表破坏了我的帖子。不过,谢谢一百万,伙计。
  • 基于我所看到的.. objectArraylistCounter 应在循环前初始化为 0,并在循环内加一。就像我在 cataCtr 中所做的那样。
  • 忘记了“objectArraylistCounter++;”谢谢你,你是个好人。
猜你喜欢
  • 1970-01-01
  • 2021-09-05
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多