让我们说我试图模仿一组股票价格。这些股票价格中的每一个都在一个数组中。 delta是每个价格之间的差异。我正在寻找一个找到最大的delta并将其返回到一个数组。当我尝试在Eclipse中实现它时,我对这两行代码产生了以下错误:
if (delta > largestDelta) {
largestDelta = stocks[i];
}
“运算符>未定义参数类型int,int []”
“类型不匹配:无法从int转换为int []”
这是我的整个代码:
public class Stocks {
static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
// delta = sell point - buy point
// find largest delta
public static int[] findMaximumProfit(int[] stocks) {
int delta = 0;
int[] largestDelta = {0};
for (int i=0; i < stocks.length; i++) {
delta = stocks[i+1] - stocks[i];
if (delta > largestDelta) {
largestDelta = stocks[i];
}
}
return largestDelta;
}
}
我可能做错了什么?
答案 0 :(得分:4)
更改
int[] largestDelta = {0};
到
int largestDelta = stocks[0];
或
int largestDelta = 0;
还要记住改变返回类型。
答案 1 :(得分:0)
问题是您正在尝试比较int和数组。这当然是一个错误:delta > largestDelta
。
另一个错误是您正在为数组分配int。此行也是错误:largestDelta = stocks[i];
您必须将最大增量更改为int,如下所示:
int largestDelta = 0;
完成此操作后,您将能够比较delta
和largestDelta
,并将stocks[i]
分配给largestDelta
希望这有帮助!
答案 2 :(得分:0)
您正在将int delta
与int [],largestDelta
进行比较。或许,您应该简单地说int largestDelta = 0;
并定义public static int findMaximumProfit(int[] stocks)
?
如果你正在寻找所有可能股票之间所有增量的最大值,你需要做一个双循环:
for (int i=0; i < stocks.length; i++) {
for (int j=i+1; j < stocks.length; j++) {
delta = stocks[j] - stocks[i];
if (delta > largestDelta) {
largestDelta[0] = i;
largestDelta[1] = j;
}
}
}
答案 3 :(得分:0)
考虑跟踪您的delta,以便了解它们的数量,并可以选择稍后进行一些额外的操作。另外,请使用Java在订购和包装方面提供的功能。
例如:
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int delta;
private int profit;
public ProfitDelta(int delta, int profit) { this.delta = delta; this.profit = profit; }
public int getDelta() { return delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) {
return o.delta - this.delta;
}
}
class Main {
static Integer[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() + ", delta=" + profit.getDelta());
}
}
public static ProfitDelta[] getBestProfitAmount(Integer[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
List<Integer> sortedStocks = Arrays.asList(stocks);
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
Collections.sort(sortedStocks);
int delta = 0;
int stockValue;
for (int i = 0; i < stocks.length; i++) {
stockValue = sortedStocks.get(i);
delta = stockValue - delta;
sortedProfits.set(i, new ProfitDelta(delta, stockValue));
delta = stockValue;
}
Collections.sort(sortedProfits);
return profits;
}
}
会给你输出:
Best amount of profit is
Profit : 5, delta=4
Profit : 10, delta=3
Profit : 1, delta=1
Profit : 6, delta=1
Profit : 7, delta=1
Profit : 6, delta=0
Profit : 7, delta=0
Profit : 10, delta=0
如果在原始数组中排序很重要,您可能希望将sortedStocks
包装到另一个保留原始索引值的类中,并将属性originalIndex
添加到ProfitDelta
。或事件更简单,如下:
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int originalIndex = 0;
private int delta = 0;
private int profit;
public ProfitDelta(int index, int profit) { this.originalIndex = index; this.profit = profit; }
public int getOriginalIndex() { return originalIndex; }
public int getDelta() { return delta; }
public void setDelta(int delta) { this.delta = delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) { return o.delta - this.delta; }
}
class ProfitComparator implements Comparator<ProfitDelta> {
@Override public int compare(ProfitDelta o1, ProfitDelta o2) {
return o1.getProfit() - o2.getProfit();
}
}
class Main {
static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() +
", delta=" + profit.getDelta() +
", originalIndex=" + profit.getOriginalIndex());
}
}
public static ProfitDelta[] getBestProfitAmount(int[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
int delta = 0;
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
for (int i = 0; i < stocks.length; i++) {
sortedProfits.set(i, new ProfitDelta(i, stocks[i]));
}
Collections.sort(sortedProfits, new ProfitComparator());
for (ProfitDelta profit : profits) {
profit.setDelta(profit.getProfit() - delta);
delta = profit.getProfit();
}
Collections.sort(sortedProfits);
return profits;
}
}
哪个输出:
Best amount of profit is
Profit : 5, delta=4, originalIndex=5
Profit : 10, delta=3, originalIndex=4
Profit : 1, delta=1, originalIndex=0
Profit : 6, delta=1, originalIndex=2
Profit : 7, delta=1, originalIndex=1
Profit : 6, delta=0, originalIndex=6
Profit : 7, delta=0, originalIndex=3
Profit : 10, delta=0, originalIndex=7