我需要编写代码来查找序列中两个整数之间的最大差。用户应该连续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);
}
}
答案 0 :(得分:2)
您正在将largeDiff
分配给diff2
。修改您的代码,如下所示:
if (diff2 > diff1) {
largeDiff = diff2;
}
else {
largeDiff = diff1;
}
您的代码也有问题。要找到最大的差异:
编辑: 做以下修改:
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;
largeDiff = Math.abs(diff1);
for (int i = 1; i <= 8; i++) {
int priceA = sc.nextInt();
int priceB = sc.nextInt();
int diff2 = Math.abs(priceA - priceB);
if (diff2 > largeDiff) {
largeDiff = diff2;
}
}
System.out.println(largeDiff);
}
N.B: Math::abs
用于查找绝对值
答案 1 :(得分:1)
用户应该连续10天输入股票价格,程序会告诉您每日最大的变化
您的代码允许用户输入10个以上的价格。您应该在用户输入下一个价格时跟踪价格差异。请参见以下算法:
import java.util.Scanner;
public class LargestDiff {
public static void main(String[] args) {
final int days = 10;
Scanner sc = new Scanner(System.in);
int largeDiff = calculateLargestDiff(sc, days);
System.out.println(largeDiff);
sc.close(); //don't forget to close scanner
}
public static int calculateLargestDiff(Scanner sc, int days){
int largeDiff = 0;
System.out.println("Enter a stock price for day 1");
int price1 = sc.nextInt();
for (int i = 2; i <= days; i++) {
System.out.println("Enter a stock price for day "+i);
int price2 = sc.nextInt();
int diff2 = Math.abs(price1 - price2);
price1 = price2;
if (diff2 > largeDiff) {
largeDiff = diff2;
}
}
return largeDiff;
}
}
您可能会注意到,我创建了第二个方法,该方法采用了它的依赖关系-Scanner
和int
作为参数。这使编写自动化测试变得更加容易。
还有其他需要考虑的地方-您的代码无法处理无效的输入。例如,如果用户输入字母而不是整数。您可能应该添加逻辑来处理这类情况。
答案 2 :(得分:0)
import java.util.Scanner;
public class Change {
public static void main (String [] args) {
final int days = 10;
int largeDiff = 0; // largest difference
int diff1 = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a stock price:");
for (int i = 0; i < 5; i++) {
int priceA = sc.nextInt();
int priceB = sc.nextInt();
int diff2 = Math.abs(priceA - priceB);
if (diff2 > diff1) {
largeDiff = diff2;
diff1 = diff2;
}
}
System.out.println(largeDiff);
}
}
答案 3 :(得分:0)
我一个人喜欢先接受输入,然后再进行所有必要的计算。您还应该从用户权限中获取10个输入吗?因此,进行一个循环运动大约8次,每次循环都从Scanner
中获取2个输入,基本上意味着您总共需要提供16个输入,这绝对不是您想要的。我的解决方案在下面给出以供参考。
public class Change
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int totalDays = 10;
int stockPrices[] = new int[totalDays];
for(int c = 1; c <= totalDays; c++) {
System.out.printf("Enter stock price for day %d\n", c);
stockPrices[c - 1] = sc.nextInt();
}
int largestDiff = -1;
for(int c = 0; c < totalDays - 1; c++) {
int diff = Math.abs(stockPrices[c] - stockPrices[c + 1]);
if(diff > largestDiff) {
largestDiff = diff;
}
}
System.out.printf("Largest difference in stock price is %d\n", largestDiff);
}
}