java显示从方法输出的单元格网格

时间:2015-11-24 10:51:40

标签: java

这里是我正在处理的代码我更新了它,从网上得到了一些代码也许是stackflow并且像解析部分一样编辑它我不了解该代码的所有内容但足以让它工作并且大部分发生了什么和Thread.Sleep,但我可以想出来,虽然基本上,我迷失了一些事情...例如用户输入有填充单元格(i,j)的用户输入值,并在displayGrid程序将计算和显示无论是“”(空格)还是“#”,我都得到了那个部分没关系,除了它在10x10网格上打印一条线比它应该的更多,例如如果它在第7行水平打印出来,它应该实际上在线6水平。此外,我现在必须使用updateGrid方法来更新网格。例如,如果填充了单元,我必须找出相邻单元。每个小区最多有8个邻居。首先,我如何计算出如何计算邻居?任何人都可以给我一些提示... Bijan

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

class Name {

    public static String name; 
}

public class Project8a {

    private static int populatedCells = 1;
    private static int unpopulatedCells = 0;


    public static void main(String[] args) throws InterruptedException, ParseException{


        //int populatedCells = 100, unpopulatedCells = 100;
        Scanner scan = new Scanner(System.in);
        int mat[][] = new int[10][10];

        //get time of day, etc...
        timeOfDay();

        System.out.println("\nPlease enter list of (i,j) pairs for populated cells (negative i or j to quit) : ");

        int i = scan.nextInt();
        int j = scan.nextInt();

        while(i >= 0 && j >= 0){

            mat[i][j] = 1;
            i = scan.nextInt();
            j = scan.nextInt();

        }


        System.out.println("Enter number of time steps : ");
        int numberOfTimeSteps = scan.nextInt();


        System.out.println("Intial Grid : \n");

        /************************************

        attempt to loop through time steps
        and try to use / test 'sleep' method
        do {
        displayGrid(mat);
        }while(mat[i][j] <= 10); 

        *************************************/
        //display and print-out 10x10 grid
        displayGrid(mat);
        //update cells within 10x10 grid 
        updateGrid(mat);

    }

        public static void displayGrid(int mat[][]){


            for (int i = 0; i < 10; i++){
                System.out.print(i);
            }
                System.out.println();
                System.out.print("          ");
            for (int i = 0; i < 10; i++){
                System.out.println(i);
            for (int j = 0; j < 10; j++){

                if(mat[i][j] == 1)
                    System.out.print("#");
                else  {
                    System.out.print(" ");

                }
            }

            /***************************

            attempt to make outer-edge
            cells = '0' 
            if(i == 0 || j == 0){
                mat[i][j] = 0;
            }

            ****************************/




        }


            } 


    public static void updateGrid(int mat[][]) 

            throws InterruptedException{
            int i = 0;
            int j = 0;
            int newArray[][] = new int[mat[i].length][mat[j].length];
            int populatedCells = 1;
            //for(b = 0; b < [mat[i].length][mat[j].length];
            //int unpopulatedCells = 2;
            int neighborCells = 8;

            if(neighborCells <= 1 || neighborCells >= 4)
                populatedCells = 0;
            else if (neighborCells == 3)
                populatedCells = 1;

            /************************************************************************************** 

                    For a cell that is “populated”, if the cell has <= 1 neighbors, 
                    or >= 4 neighbors, it  dies (becomes 0). Otherwise, 
                    it survives (remains 1).   For a cell that is not populated, 
                    if the cell has exactly 3 neighbors, it becomes  populated (becomes 1). 
                    Cells on the edge always remain unpopulated (0).

            **************************************************************************************/

            System.out.println("\n");
            System.out.print("Now testing sleep method (for 5 seconds) : ");
            System.out.println();
            System.out.println();
            Thread.sleep(1000);
            System.out.println("5");
            Thread.sleep(1000);
            System.out.println("4");
            Thread.sleep(1000);
            System.out.println("3");
            Thread.sleep(1000);
            System.out.println("2");
            Thread.sleep(1000);
            System.out.println("1");
            Thread.sleep(1000);
            System.out.print("0");
            Thread.sleep(1000);
            System.out.print(".");
            Thread.sleep(1000);
            System.out.print(".");
            Thread.sleep(1000);
            System.out.print(".\n");
            Thread.sleep(2500);
            System.out.print("\nBlast!!! It worked!!!\n\n");
            Thread.sleep(4000);
            System.out.println("Ah you thought it was over HAHA!!!");
            System.out.println("Actually that was six seconds!!!\n");
            Thread.sleep(1000);
            System.out.print("S");
            Thread.sleep(750);
            System.out.print("E");
            Thread.sleep(750);
            System.out.print("E" + " ");
            Thread.sleep(750);
            System.out.print("Y");
            Thread.sleep(750);
            System.out.print("A");
            Thread.sleep(750);
            System.out.print("H");
            Thread.sleep(750);
            System.out.print("!");
            Thread.sleep(750);
            System.out.print("!");
            Thread.sleep(750);
            System.out.print("!" + " ");
            Thread.sleep(1000);

            for (int c = 0; c < Name.name.length(); c++) {
                System.out.print(Name.name.charAt(c));
                Thread.sleep(750L); 
            }



        }public static int timeOfDay() throws ParseException{

            Scanner scan = new Scanner(System.in);
            System.out.println("First off,  please enter your name for the database storage : ");
            Name.name = scan.nextLine();

            Date date = new Date() ;
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm") ;
            dateFormat.format(date);
            //System.out.println(dateFormat.format(date));

            if(dateFormat.parse(dateFormat.format(date)).after(dateFormat.parse("6:00"))&& dateFormat.parse(dateFormat.format(date)).before(dateFormat.parse("11:59")))
            {
                System.out.println("\nOkay " + Name.name + ", hope you're having a good morning - lets play!!!");
            }
            else if(dateFormat.parse(dateFormat.format(date)).after(dateFormat.parse("11:59"))&& dateFormat.parse(dateFormat.format(date)).before(dateFormat.parse("17:00")))
            {
                System.out.println("\nOkay " + Name.name + ", hope you're having a good afternoon - lets play!!!");
            }
            else if(dateFormat.parse(dateFormat.format(date)).after(dateFormat.parse("17:00"))&& dateFormat.parse(dateFormat.format(date)).before(dateFormat.parse("18:59")))
            {
                System.out.println("\nOkay " + Name.name + ", hope you're having a good evening - lets play!!!");
            }
            else if(dateFormat.parse(dateFormat.format(date)).after(dateFormat.parse("18:59"))&& dateFormat.parse(dateFormat.format(date)).before(dateFormat.parse("23:59")))
            {
                System.out.println("\nOkay " + Name.name + ", hope you're having a good night so far - lets play!!!");
            }
            return populatedCells;


        }

}

1 个答案:

答案 0 :(得分:0)

使用列表而不是2D数组可能更清楚,因此简化了一点:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Project8a {
    public static void main(String[] args){

        System.out.println("Please enter list of (i,j) pairs for populated cells (negative i or j to quit) : ");

        List<Cell> cells = new ArrayList<Cell>();

        try (Scanner scan = new Scanner(System.in)) {
            while (true) {
                int one =  scan.nextInt();
                if (one < 0) break;

                int two =  scan.nextInt();
                if (two < 0) break;            

                cells.add(new Cell(one, two)); 
            }
        }

        System.out.println("Intial Grid : ");
        for (Cell cell : cells) {
            System.out.println(cell);
        }
    }

    static class Cell {
        private int one, two;
        Cell(int one, int two) { this.one = one; this.two = two; }
        public void setOne(int one) { this.one = one; }
        public void setTwo(int two) { this.two = two; }
        public int getOne() { return one; }
        public int getTwo() { return two; }

        public String toString() {
            return "[ " + one + ", " + two + " ]";          
        }
    }
}