Java作业解决请求:连续相等的数字

huangapple go评论73阅读模式
英文:

Java Assignment Solution Request: Consecutive Equal Numbers

问题

这个Java作业让我头疼,我希望有人能帮我解决并解释他们是如何找到答案的。我已经尝试了好几个小时,但无法理解它:/ 真糟糕!

作业:连续相等的数字

问题描述:

编写以下函数,测试二维列表是否具有四个连续相等值的数字,无论是水平、垂直还是对角线方向。

public static boolean isConsecutiveFour(int[][] values)

编写一个测试程序,提示用户输入二维列表的行数和列数,然后输入列表中的值。如果列表包含四个连续相同值n,程序将显示“找到四个连续的n”。否则,它将显示“未找到四个连续的出现”。

我尽力尝试理解数组,但术语和数字太多,弄得我头都大了:/

英文:

This java assignment is frying my brain and I was hoping someone could solve this for me and explain how they found the answer? I have been trying for hours to comprehend it :/ this stinks!

Assignment: Consecutive Equal Numbers

Problem Description:

Write the following function that tests whether a two-dimensional list has four consecutive numbers of the same value, either horizontally, vertically, or diagonally.

public static boolean isConsecutiveFour(int[][] values) 

Write a test program that prompts the user to enter the number of rows and columns of a two-dimensional list and then the values in the list. The program will display
Found four consecutive occurrences of n.
if the list contains four consecutive numbers with the same value n. Otherwise, it will display
Did not find four consecutive occurrences.

I pretty much tried to understand arrays but there is so much terminology and numbers and it destroyed my mind :/

答案1

得分: 0

以下是您提供的代码的翻译部分:

这是在基本的 5 x 5 输入上进行测试,并且有效。

这只是 for 循环 和条件检查。
如果您有任何问题,请告诉我——我在每个测试上都添加了注释。

public class Example {
    public static void main(String[] args) {
        ui();
    }

    static int rows, columns;

    static void ui() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入行和列:");
        rows = scanner.nextInt();
        columns = scanner.nextInt();
        int[][] values = new int[rows][columns];
        for (int row = 0; row < rows; row++) {
            System.out.printf("输入第 %d 行:", row + 1);
            for (int column = 0; column < columns; column++)
                values[row][column] = scanner.nextInt();
        }
        System.out.println("isConsecutiveFour(values) = " + isConsecutiveFour(values));
    }

    public static boolean isConsecutiveFour(int[][] values) {
        /* 检查水平方向 */
        int previous, count;
        for (int[] row : values) {
            previous = row[0];
            count = 0;
            for (int index = 1; index < row.length; index++) {
                if (previous == row[index]) count++;
                if (count == 3) return true;
                previous = row[index];
            }
        }
        /* 检查垂直方向 */
        for (int column = 0; column < columns; column++) {
            previous = values[0][column];
            count = 0;
            for (int row = 1; row < rows; row++) {
                if (previous == values[row][column]) count++;
                if (count == 3) return true;
                previous = values[row][column];
            }
        }
        /* 检查水平方向,从左到右 */
        for (int column = 0; column < columns; column++) {
            previous = values[0][column];
            count = 0;
            for (int row = 1; row < rows; row++) {
                if (column + 1 >= columns) break;
                if (previous == values[row][++column]) count++;
                if (count == 3) return true;
                previous = values[row][column];
            }
        }
        /* 检查水平方向,从右到左 */
        for (int column = 0; column < columns; column++) {
            previous = values[rows - 1][column];
            count = 0;
            if (rows - 2 < 0) return false;
            for (int row = rows - 2; row >= 0; row--) {
                if (column + 1 >= columns) break;
                if (previous == values[row][++column]) count++;
                if (count == 3) return true;
                previous = values[row][column];
            }
        }
        return false;
    }
}

示例输入和输出。

这些应该通过。

输入行和列:5 5
输入第 1 行:0 1 2 3 4
输入第 2 行:4 0 1 2 3
输入第 3 行:3 4 0 1 2
输入第 4 行:2 3 4 0 1
输入第 5 行:1 2 3 4 0
isConsecutiveFour(values) = true
输入行和列:5 5
输入第 1 行:1 2 3 4 5
输入第 2 行:1 2 3 4 5
输入第 3 行:0 0 0 0 0
输入第 4 行:1 2 3 4 5
输入第 5 行:1 2 3 4 5
isConsecutiveFour(values) = true
输入行和列:5 5
输入第 1 行:1 2 0 3 4
输入第 2 行:1 2 0 3 4
输入第 3 行:2 1 0 4 3
输入第 4 行:1 2 0 3 4
输入第 5 行:1 2 0 3 4
isConsecutiveFour(values) = true

而这些应该失败。

输入行和列:5 5 
输入第 1 行:1 2 3 4 5
输入第 2 行:5 4 3 2 1
输入第 3 行:0 1 0 1 0
输入第 4 行:5 4 3 2 1
输入第 5 行:1 2 3 4 5
isConsecutiveFour(values) = false
输入行和列:3 3
输入第 1 行:1 1 1
输入第 2 行:1 1 1
输入第 3 行:1 1 1
isConsecutiveFour(values) = false

测试用例

输入行和列:6 7
输入第 1 行:0 1 0 3 1 6 1
输入第 2 行:0 1 6 8 6 0 1
输入第 3 行:5 6 2 1 8 2 9
输入第 4 行:6 5 6 1 1 9 1
输入第 5 行:1 3 6 1 4 0 7
输入第 6 行:3 3 3 3 4 0 7
isConsecutiveFour(values) = true
英文:

This is tested on basic 5 x 5 inputs, and works.

It's just for-loops and conditional checks.
Let me know if you have any questions&mdash;I added comments above each test.

public class Example {
    public static void main(String[] args) {
        ui();
    }

    static int rows, columns;

    static void ui() {
        Scanner scanner = new Scanner(System.in);
        System.out.print(&quot;enter rows and columns:  &quot;);
        rows = scanner.nextInt();
        columns = scanner.nextInt();
        int[][] values = new int[rows][columns];
        for (int row = 0; row &lt; rows; row++) {
            System.out.printf(&quot;enter row %d:  &quot;, row + 1);
            for (int column = 0; column &lt; columns; column++)
                values[row][column] = scanner.nextInt();
        }
        System.out.println(&quot;isConsecutiveFour(values) = &quot; + isConsecutiveFour(values));
    }

    public static boolean isConsecutiveFour(int[][] values) {
        /* check horizontal */
        int previous, count;
        for (int[] row : values) {
            previous = row[0];
            count = 0;
            for (int index = 1; index &lt; row.length; index++) {
                if (previous == row[index]) count++;
                if (count == 3) return true;
                previous = row[index];
            }
        }
        /* check vertical */
        for (int column = 0; column &lt; columns; column++) {
            previous = values[0][column];
            count = 0;
            for (int row = 1; row &lt; rows; row++) {
                if (previous == values[row][column]) count++;
                if (count == 3) return true;
                previous = values[row][column];
            }
        }
        /* check horizontal, left to right */
        for (int column = 0; column &lt; columns; column++) {
            previous = values[0][column];
            count = 0;
            for (int row = 1; row &lt; rows; row++) {
                if (column + 1 &gt;= columns) break;
                if (previous == values[row][++column]) count++;
                if (count == 3) return true;
                previous = values[row][column];
            }
        }
        /* check horizontal, right to left */
        for (int column = 0; column &lt; columns; column++) {
            previous = values[rows - 1][column];
            count = 0;
            if (rows - 2 &lt; 0) return false;
            for (int row = rows - 2; row &gt;= 0; row--) {
                if (column + 1 &gt;= columns) break;
                if (previous == values[row][++column]) count++;
                if (count == 3) return true;
                previous = values[row][column];
            }
        }
        return false;
    }
}

Example input and outputs.

These should pass.

enter rows and columns:  5 5
enter row 1:  0 1 2 3 4
enter row 2:  4 0 1 2 3
enter row 3:  3 4 0 1 2
enter row 4:  2 3 4 0 1
enter row 5:  1 2 3 4 0
isConsecutiveFour(values) = true
enter rows and columns:  5 5
enter row 1:  1 2 3 4 5
enter row 2:  1 2 3 4 5
enter row 3:  0 0 0 0 0
enter row 4:  1 2 3 4 5
enter row 5:  1 2 3 4 5
isConsecutiveFour(values) = true
enter rows and columns:  5 5
enter row 1:  1 2 0 3 4
enter row 2:  1 2 0 3 4
enter row 3:  2 1 0 4 3
enter row 4:  1 2 0 3 4
enter row 5:  1 2 0 3 4
isConsecutiveFour(values) = true

And, these should fail.

enter rows and columns:  5 5 
enter row 1:  1 2 3 4 5
enter row 2:  5 4 3 2 1
enter row 3:  0 1 0 1 0
enter row 4:  5 4 3 2 1
enter row 5:  1 2 3 4 5
isConsecutiveFour(values) = false
enter rows and columns:  3 3
enter row 1:  1 1 1
enter row 2:  1 1 1
enter row 3:  1 1 1
isConsecutiveFour(values) = false

Test case

enter rows and columns:  6 7
enter row 1:  0 1 0 3 1 6 1
enter row 2:  0 1 6 8 6 0 1
enter row 3:  5 6 2 1 8 2 9
enter row 4:  6 5 6 1 1 9 1
enter row 5:  1 3 6 1 4 0 7
enter row 6:  3 3 3 3 4 0 7
isConsecutiveFour(values) = true

huangapple
  • 本文由 发表于 2023年6月9日 06:01:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定