[英]Logic Error/Output Issue Involving 2d-Array
我不太确定错误在代码中的什么地方发生(否则我会自己修复)。 但是-对我的输出产生负面影响
我相信这可能是错误的汇总-此外,我相当确定我的课程运行正常(我认为 ),而其测试程序是错误的来源。 下面显示了类及其测试器(为了准确地重现输出及其错误)。 这是我期望计算的输出:
非常感谢您的帮助-我真的很茫然。
/**
* 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++;
}
}
}
在二维数组上使用计数器很麻烦。 我建议您将其更改为嵌套循环。即
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++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.