gpt4 book ai didi

java - 涉及 2d 数组的逻辑错误/输出问题

转载 作者:行者123 更新时间:2023-12-01 13:38:06 26 4
gpt4 key购买 nike

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

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

最佳答案

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

关于java - 涉及 2d 数组的逻辑错误/输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21089538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com