我需要帮助来编写一段代码,找出一个序列中两个整数之间的最大差异

I need help to write a code that finds the largest difference between two integers in a sequence

我需要编写一段代码,找出序列中两个整数之间的最大差异。用户应该输入连续 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);

}

}

您正在将 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用于求绝对值

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);

}
}

The user is supposed to input the stock prices from 10 consecutive days and the program will tell you the biggest day to day change

您的代码允许用户输入超过 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;
    }
}

您可能会注意到我创建了第二个方法,该方法采用其依赖项 - Scannerint 作为参数。这使得编写自动化测试变得更加容易。

需要考虑的其他事项 - 您的代码不处理无效输入。例如,如果用户输入字母而不是整数。您可能应该添加逻辑来处理这些场景。

我喜欢先接受输入,然后进行所有必要的计算。您还应该从用户那里获取 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);
    }
}