使项目更加OO Java

时间:2013-11-14 18:15:30

标签: java arrays loops methods

我已经创建了一个12到12的表,它运行在同一个程序中,但是我想把它设为OO以便我可以将“public static void main”放在另一个“testfile”中它仍然可以运行正确..我对OO方法有一些问题,我真的需要帮助......这就是我的代码:

import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;

public class PlayingArea {
    public static void main(String[] args) {
        Random r = new Random();
        Scanner input = new Scanner(System.in);
        System.out.println("How many regions would you like (2- 4)");
        int region = input.nextInt();
        String letters = "";
        while(letters.length() < 2) {
            if (region == 4) {
                letters= "EFGH";
            }
            if (region == 3) {
                letters= "EFG";
            } else if (region == 2) {
                letters= "EF";
            } else if (region < 2) {
                System.out.println("You inputed a wrong value, try again...");
            }
        }
        int N = letters.length();
        char [][] letter = new char[12][12];

        for (int j = 0; j < letter.length; j++) {

            for(int i=0; i < letter.length; i++) {
                letter[i][j] = letters.charAt(r.nextInt(N)) ;
            }
        }

        for (char[] letterRow : letter)
            System.out.println(Arrays.toString(letterRow));
    }
}

1 个答案:

答案 0 :(得分:2)

如果你对java比较陌生,那你就做得很好。请注意,如果输入2-4之外的数字,程序中会出现无限循环(在下面修复)。

首先,您的类PlayingArea需要一些成员变量来表示状态。

第一个是String letters(EF或EFG或EFGH),它是通过下面代码中的构造函数初始化的。

第二个是char[][] grid(在代码中从letter重命名),在populate()方法中分配了一个值,该字母将字母放入网格中。

另一种方法,gridAsString()就是这样做的。

如果您愿意,public static void main现在可以很容易地移到另一个班级。

玩得开心。

public class PlayingArea {
    private String letters;
    private char[][] grid;

    public PlayingArea(String letters) {
        this.letters = letters;
    }

    public void populate() {
        int n = letters.length();
        grid = new char[12][12];

        Random r = new Random();
        for (int j = 0; j < grid.length; j++) {
            for (int i = 0; i < grid.length; i++) {
                grid[i][j] = letters.charAt(r.nextInt(n));
            }
        }

    }

    public String gridAsString() {
        StringBuilder sb = new StringBuilder();
        for (char[] letterRow : grid) {
            sb.append(Arrays.toString(letterRow)).append('\n');
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("How many regions would you like (2- 4)");
        String letters = "";
        while (letters.length() < 2) {
            int region = input.nextInt();
            if (region == 4) {
                letters = "EFGH";
            } else if (region == 3) {
                letters = "EFG";
            } else if (region == 2) {
                letters = "EF";
            } else {
                System.out.println("You inputed a wrong value, try again...");
            }
        }
        PlayingArea playingArea = new PlayingArea(letters);
        playingArea.populate();
        System.out.println(playingArea.gridAsString());
    }
}