我需要编写代码来查找序列中两个整数之间的最大差。用户应该连续10天输入股票价格,程序会告诉您每天最大的变化。我被卡住了。
import java.util.Scanner;
public class Change {
public static void main (String [] args) {
final int days = 10;
int largeDiff = 0; // largest difference
Scanner sc = new Scanner(System.in);
System.out.println("Enter a stock price:");
int price1 = sc.nextInt();
int price2 = sc.nextInt();
int diff1 = price1 - price2;
for (int i = 1; i <= 8; i++) {
int priceA = sc.nextInt();
int priceB = sc.nextInt();
int diff2 = priceA - priceB;
if (diff2 > diff1) {
diff2 = largeDiff;
}
else {
diff2 = diff1;
}
}
System.out.println(largeDiff);
}
}
相关分类