所以我有一个接口,它是由一个名为Vehicle的类实现的,它接受一个双精度值作为参数:
public class Vehicle implements Efficiency
{
//instance variable
private double efficiency;
public Vehicle(double x)
{
//parameter initializes the instance variable
x = efficiency;
}这是一个界面:
public interface Efficiency
{
double getEfficiency();
}我需要创建一个名为getFirstBelowT()的方法,它接受一个效率数组和一个双精度数组作为参数。该方法应返回一个效率对象,该对象是数组中的第一个小于参数中的双精度值的对象。如何将效率数组中的元素与双精度值进行比较?这就是我到目前为止所知道的:
//a method that returns an Efficiency object that is the first
//object in the array with a efficiency below the double parameter
public static Efficiency getFirstBelowT(Efficiency[] x, double y)
{
//loop through each value in the array
for(Efficiency z: x)
{
//if the value at the index is less than the double
if(z < y)
{ //returns the first value that is less than y and stops looping
// through the array.
return z;
break;
}
}
//returns null if none of the element values are less than the double
return null;
}发布于 2015-10-26 07:59:59
你基本上做到了:
getEfficiency方法:if(z.getEfficiency()
break之后的return:这是无法访问的代码。https://stackoverflow.com/questions/33336450
复制相似问题