- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这些类旨在协同工作以产生如下输出:/image/FPYwn.png我有一个逻辑错误,不允许我为显示和/或声明为 0 的变量生成输出和数据。我已经拼命工作了几个小时试图修复这个错误——但没有成功。如果有人能够告诉我如何解决代码中的问题以产生我想要的输出,我将非常感激。
这就是类(class)
/**
* This class instantiates car objects with ten private instance variables.
* It contains eight mutator methods to calculate mi/gal, distance (mi), gal/mi,
* total cost; minimum/maximum, total, and annual values for these variables within a car object.
* There are getter methods for each private instance variable.
*
* @author A. Mackey
* @version 01/02/14
*/
public class AnnualFuelUse
{
//private instance variables
private double gallonsUsed, pricePerGallon, totalCost,
minSpeed, maxSpeed,
minVariable, maxVariable, minMaxVariable,
galPerMi, miPerGal,
startingMiles, endingMiles, distance,
totalVariable[], totalValue;
//constructor with two parameters
AnnualFuelUse(int FillUpNumber, double eMiles, double sMiles, double galsUsed, double pricePerGal)
{
startingMiles = sMiles;
endingMiles = eMiles;
gallonsUsed = galsUsed;
pricePerGallon = pricePerGal;
}
//mutator method which calculates the distance drive
public void calcDistance()
{
distance = endingMiles - startingMiles;
}
//getter method to return the value of distance traveled by the object on one trip
public double getDistance()
{
return distance;
}
//mutator method which calculates gallons used per mile driven
public void calcMPG(double dist, double gallons)
{
miPerGal = dist / gallons;
}
//getter method to return the value of the miles per gallon of the object on one trip
public double getMPG()
{
return miPerGal;
}
//mutator method which calculates miles driven per gallons used
public void calcGPM(double gallons, double dist)
{
galPerMi = gallons / dist;
}
//getter method to return the value of the gallons per mile of the object on one trip
public double getGPM()
{
return galPerMi;
}
//mutator method which calculates total cost of filling up
public void totalCost()
{
totalCost = pricePerGallon * gallonsUsed;
}
//getter method to return the value of the total gasoline purchases on one trip
public double getTotalCost()
{
return totalCost;
}
//mutator method which calculates the minimum value of a given array
public void min(double minMaxVariable[])
{
minVariable = Double.MAX_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) < minVariable)
{
minVariable = minMaxVariable[i];
}
}
}
//getter method to return the minimum value of a given array
public double getMin()
{
return minVariable;
}
//mutator method which calculates the minimum value of a given array
public void max(double minMaxVariable[])
{
maxVariable = Double.MIN_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) > maxVariable)
{
maxVariable = minMaxVariable[i];
}
}
}
//getter method to return the minimum value of a given array
public double getMax()
{
return maxVariable;
}
//mutator method which calculates the total value of a given array
public void totalOf(double totalVariable[])
{
totalValue = 0;
for(double newValue : totalVariable)
{
totalValue += newValue;
}
}
//getter method to return the total value of a given array
public double getTotalOf()
{
return totalValue;
}
}
及其测试器类
/**
* This class tests the AnnualFuelUse class.
*
* An array of car objects is created to hold some of the private instance variables.
*
* A for loop is used to call the variables' methods on each object.
* A second for loop is used to print an output header identifying the variables for the instance
* A third for loop is used to print the values of the instance variables for each object.
*
* @author A. Mackey
* @version 01/02/14
*/
public class AnnualFuelUseTester
{
//main method
public static void main(String[] args)
{
//declaring and initialising variables
String header = "Annual Gas Mileage Calculations",
headerLine = "===============================";
String[] headerInfo = { "Fill Up" , " Days" , " Start Miles" , " End Miles" , "Distance" , "Gallons" , " Price" , "Cost" , " Miles/Gal" , "Gal/Mile" };
int[] fillUpNumber = {1, 2, 3, 4},
daysSinceFill = {1, 4, 8, 13};
double[] startMiles = {24963, 25437, 25937, 26221},
endMiles = { 25437, 25937, 26221, 26794},
gallonsUsed = { 21.4, 21.2, 12.2, 23.7},
milesPerGallon = new double[startMiles.length],
gallonsPerMile = new double[startMiles.length],
pricePerGallon = {3.52, 3.45, 3.39, 3.41},
totalCost = new double[startMiles.length],
distance = new double[startMiles.length];
int minDistance,
maxDistance,
totalDistance,
annualDistance;
double minMPG, minCost,
maxMPG, maxCost,
totalGallonsUsed, totalCostSum,
annualGallonsUsed, annualCost, annualMPG;
//initialization of array of objects
AnnualFuelUse[] car = {new AnnualFuelUse(fillUpNumber[0], endMiles[0], startMiles[0], gallonsUsed[0], pricePerGallon[0]),
new AnnualFuelUse(fillUpNumber[1], endMiles[1], startMiles[1], gallonsUsed[1], pricePerGallon[1]),
new AnnualFuelUse(fillUpNumber[2], endMiles[2], startMiles[2], gallonsUsed[2], pricePerGallon[2]),
new AnnualFuelUse(fillUpNumber[3], endMiles[3], startMiles[3], gallonsUsed[3], pricePerGallon[3])};
//call methods
for(int index = 0; index < car.length; index++)
{
distance[index] = car[index].getDistance();
milesPerGallon[index] = car[index].getMPG();
gallonsPerMile[index] = car[index].getGPM();
totalCost[index] = car[index].getTotalCost();
}
//I desire to invoke the methods which relate to these particular variables in order to define them
minDistance = 0;
minMPG = 0;
minCost = 0;
maxDistance = 0;
maxMPG = 0;
maxCost = 0;
totalDistance = 0;
totalGallonsUsed = 0;
totalCostSum = 0;
annualDistance = 0;
annualGallonsUsed = 0;
annualCost = 0;
annualMPG = 0;
//print results
System.out.printf("%74s%n", header);
System.out.printf("%74s%n", headerLine);
for(String info : headerInfo)
{
System.out.print(info + "\t");
}
System.out.println("\n=========================================================================================================================");
for(int index = 0; index < car.length; index++)
{
System.out.printf("%4d%10d%14.0f%14.0f%12.0f%16.1f%11.2f%12.2f%12.2f%13.3f%n", fillUpNumber[index], daysSinceFill[index], startMiles[index], endMiles[index], distance[index], gallonsUsed[index], pricePerGallon[index], totalCost[index], milesPerGallon[index], gallonsPerMile[index]);
}
System.out.println("=========================================================================================================================\n");
System.out.printf("Minimum:%46d%27.2f%24.2f%n", minDistance, minMPG, minCost);
System.out.printf("Maximum:%46d%27.2f%24.2f%n%n", maxDistance, maxMPG, maxCost);
System.out.printf("Totals:%47d%16.1f%35.2f%n", totalDistance, totalGallonsUsed, totalCostSum);
System.out.printf("Annual Projection:%36d%16.1f%11.2f%24.2f%n", annualDistance, annualMPG, annualGallonsUsed, annualCost);
}
}
感谢您的帮助,谢谢。
编辑(美国东部时间 2014 年 1 月 3 日 03:00)
pastebin.com/BRJunSvS 和pastebin.com/kRRb0dmu 知道为什么我的 MPG 不是预计的年平均值吗???
最佳答案
距离、成本、每加仑英里数和每英里加仑数字段尚未初始化,因为您尚未调用任何方法来计算这些值。
无论如何,我认为存储基于其他实例变量计算的任何值没有多大意义。为什么不直接在调用 getDistance() 时计算距离,然后返回计算值呢?这样您就可以少维护一个字段。
这又是你的类,这次删除了一些实例变量和 setter,以及一些更改的 getter。我擅自将您的 min
、max
和 totalOf
方法更改为静态方法。我不知道这是否是一个好的做法,但它们似乎不需要访问实例级变量或方法。
public class AnnualFuelUse
{
//private instance variables
private double gallonsUsed, pricePerGallon, startingMiles, endingMiles;
//constructor with two parameters
AnnualFuelUse(int FillUpNumber, double eMiles, double sMiles, double galsUsed, double pricePerGal)
{
startingMiles = sMiles;
endingMiles = eMiles;
gallonsUsed = galsUsed;
pricePerGallon = pricePerGal;
}
//getter method to return the value of distance traveled by the object on one trip
public double getDistance()
{
return endingMiles - startingMiles;
}
//getter method to return the value of the miles per gallon of the object on one trip
public double getMPG()
{
return getDistance() / gallonsUsed;
}
//getter method to return the value of the gallons per mile of the object on one trip
public double getGPM()
{
return gallonsUsed / getDistance();
}
//getter method to return the value of the total gasoline purchases on one trip
public double getTotalCost()
{
return pricePerGallon * gallonsUsed;
}
//mutator method which calculates the minimum value of a given array
public static double min(double minMaxVariable[])
{
double minVariable = Double.MAX_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) < minVariable)
{
minVariable = minMaxVariable[i];
}
}
return minVariable;
}
//mutator method which calculates the minimum value of a given array
public static double max(double minMaxVariable[])
{
double maxVariable = Double.MIN_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) > maxVariable)
{
maxVariable = minMaxVariable[i];
}
}
return maxVariable;
}
//mutator method which calculates the total value of a given array
public static double totalOf(double totalVariable[])
{
double totalValue = 0;
for(double newValue : totalVariable)
{
totalValue += newValue;
}
return totalValue;
}
}
这应该可以处理表格上部的零。至于最小值、最大值和总值,它们显然将为零,因为您已经这样设置了它们。假设您已将上述方法设为静态,您可以这样调用它们。请注意由于 min
、max
和 totalOf
返回 double
类型的值而进行的转换。
minDistance = (int)AnnualFuelUse.min(distance);
minMPG = AnnualFuelUse.min(milesPerGallon);
minCost = AnnualFuelUse.min(totalCost);
maxDistance = (int)AnnualFuelUse.max(distance);
maxMPG = AnnualFuelUse.max(milesPerGallon);
maxCost = AnnualFuelUse.max(totalCost);
totalDistance = (int)AnnualFuelUse.totalOf(distance);
totalGallonsUsed = AnnualFuelUse.totalOf(gallonsUsed);
totalCostSum = AnnualFuelUse.totalOf(totalCost);
关于java - 将数组数据输入到与另一个类中的另一个方法不同的类中的方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897028/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!