2015-11-24 36 views
0

這裏是我正在工作的代碼我更新了它,從網上可能有一些代碼stackflow並編輯它像解析部分我不明白關於該代碼的一切,但足以得到它工作和大部分正在發生的事情和Thread.Sleep,但我可以弄清楚,雖然基本上,我失去了一些東西...例如,用戶輸入具有用戶輸入值填充細胞(我,j),在displayGrid中,程序會計算並顯示一個「」(空格)或「#」,我知道那部分沒有問題,除了它在10x10網格上打印一行比它應該更多,例如,如果打印出來7水平地,它實際上應該在水平線6上。此外,我必須現在使用updateGrid方法來更新網格。例如,如果單元格填充,我必須找出鄰居單元格。每個小區最多有8個鄰居。首先如何計算如何計算鄰居?誰能給我一些提示,請...贊java顯示網格的單元格輸出方法

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; 


     } 

} 
+0

這件事是更新容易一個二維數組單元格並將其打印出來。什麼阻止你? – user3437460

回答

0

這可能是更清晰的使用列表,而不是一個二維數組,因此簡化了一點:

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 + " ]";   
     } 
    } 
} 
+0

理查德該項目和老師要求使用多維數組爲這個項目...和相當多的東西,你正在使用你的代碼,我還沒有學到,但我不想編碼打破。 ..後面的代碼將不斷更新單元格並更改它們的位置......但感謝您試圖幫助我,Bijan – Bjon123abc