创建方法必须检查两个数中哪个较大,并执行从小到大的递增计数

Creating a method must check which of the two numbers is larger, and execute an increasing count from the smallest to the largest

好的,所以我需要在 LogicalOp class 中创建一个方法,它将接收两个数字参数。该方法必须检查两个数字中哪个较大,并执行从最小到最大的递增计数。 (eg:如果x是第一个参数,int y是第二个,如果x大于y,那么计数就是从y到x)。

我尝试过不同的方法,但它们要么什么也没做,要么陷入无限循环。我不知道如何停止从 x 到 y 的循环,如果 x 小于 y,并且从 x 开始计数到 ​​y,然后停止到我在控制台中输入的最大数字。

public void getForthExercise() {
    System.out.println("Give the x parameter and y ");
    Scanner in = new Scanner(System.in);
    int x = in.nextInt();
    int y = in.nextInt();
    if (x > y) {
        for (int i = x; i >= y; i++)
            System.out.println(i);

    } else if (x < y) {
        for (int i = y; i >= x;i++ )
            System.out.println(i);

所以如果我输入 x=25 和 y=5 ||是

让我们给你一个如何简化问题的提示:你不需要关心循环的 x < y 或 y > x。

你只关心:那个对的小数,大数!

换句话说:您只需从 min(x, y) 循环到 max(x, y)。

想一想:对于需要打印的东西,x 是 5 还是 25,或者 y 是 25 还是 5,真的重要吗?不,唯一重要的是:你有 5 个和 25 个。哪个先出现,哪个先出现,这不会改变预期输出的任何内容!

看看这个:

int min = Math.min(x, y);
int max = Math.max(x, y);
for(int i = min; i < max; i++) {
    System.out.println(i);
}