[英]How to iterate to find the lowest value
Struggling to understand where I went wrong with the iteration at the get best fare method The array holds [5.77, 2.44, 2.35] and should return the second index, however it seems that it is stuck at the double lowestPrice = lowestPriceRide[0];努力了解我在获取最佳票价方法的迭代中出错的地方该数组包含 [5.77, 2.44, 2.35] 并且应该返回第二个索引,但是它似乎被困在双最低价格 = 最低价格骑行 [0];
I thought that maybe I was putting the return out of scope, but it didn't work.我想也许我把 scope 的退货放出来了,但它没有用。
> import java.lang.*;
import java.util.Arrays;
public class TransitCalculator {
double numberOfDays = 0.0;
double numberOfRides = 0.0;
double pricePerRide = 2.75;
double pricePerWeek = 33.00;
double priceUnlimited = 127.00;
double perRide = 0.00;
public TransitCalculator(double days, double rides){
numberOfDays = days;
numberOfRides = rides;
}
public double unlimited7Price(){
double numOfWeeks = Math.ceil(numberOfDays/7) ; // Math.ceil will return the largest integer that is divisble without a remainder //
double totalPrice = numOfWeeks * pricePerWeek;
return totalPrice / numberOfRides;
}
public double[] getRidePrices(){ // 28/06/2020 Sunday. Math is verified.
double perRide = pricePerRide * numberOfRides / numberOfDays;
double perWeek = unlimited7Price();
double unlimited = priceUnlimited / numberOfRides;
double ridePrices[]; // Declared Array //
ridePrices = new double[] {perRide, perWeek, unlimited}; // New array, with added elements. Could be a mistake since I failed to declare elements//
return ridePrices;
}
public String getBestFare(){ // Error in the iteration and lowest value find! //
double lowestPriceRide[];
lowestPriceRide = getRidePrices();
double lowestPrice = lowestPriceRide[0];
for(int i = 0; i< lowestPriceRide.length; i++) {
if (lowestPrice < lowestPriceRide[i]) {
lowestPriceRide[i] = lowestPrice;
}
}
if(lowestPrice == lowestPriceRide[0]){
System.out.println("You should take the 'Pay per Ride' option in our NYC transit");
}
else if(lowestPrice == lowestPriceRide[1]){
System.out.println("You should take the 'Weekly Unlimited' plan in our NYC Transit");
}
else if(lowestPrice == lowestPriceRide[2]){
System.out.println("You should take the Unlimited ride plan in our NYC Transit");
}
return "at " + lowestPrice + "$ per Ride";
}
public static void main(String[] args){
TransitCalculator test = new TransitCalculator(26, 54);
System.out.println(test.getBestFare()); //
}
}
You are not setting the right value;您没有设置正确的值; currently, you set the element in the array to the lowest price instead of setting the lowest price to the element of the array.
目前,您将数组中的元素设置为最低价格,而不是将最低价格设置为数组元素。 You also compare against the wrong value;
您还与错误的值进行比较; you should check that the current array element is less than the best price, instead of the other way around.
您应该检查当前数组元素是否低于最佳价格,而不是相反。
Change改变
if(lowestPrice < lowestPriceRide[i])
lowestPriceRide[i] = lowestPrice;
To至
if(lowestPriceRide[i] < lowestPrice)
lowestPrice = lowestPriceRide[i];
See the updated code in action here .在此处查看更新的代码。
Note that it is unnecessary to import java.lang
, as the package is implicitly imported.请注意,没有必要导入
java.lang
,因为 package 是隐式导入的。
The problem is in your if
condition:问题出在您的
if
条件下:
if (lowestPrice < lowestPriceRide[i]) {
lowestPriceRide[i] = lowestPrice;
}
You need to see if the current lowestPriceRide[i]
is less than the already existing lowestPrice
then update your existing lowestPrice
.您需要查看当前的
lowestPriceRide[i]
是否小于现有的lowestPrice
,然后更新您现有的lowestPrice
。 So the condition would be now:所以现在的条件是:
if (lowestPriceRide[i] < lowestPrice) {
lowestPrice = lowestPriceRide[i];
}
This should be your comparison for lowest price:这应该是您对最低价格的比较:
double lowestPrice = lowestPriceRide[0];
for(int i = 0; i< lowestPriceRide.length; i++) {
if (lowestPriceRide[i] < lowestPrice) {
lowestPrice = lowestPriceRide[i];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.