Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class A9 {
- public static void main(String[] args) {
- int trees;
- double density;
- int treecount=0;
- int burncount=0;
- int burncount2=0;
- Scanner scan=new Scanner(System.in);
- //Error checking for number of trees
- do
- { System.out.print("Enter number of trees: ");
- trees=scan.nextInt();
- } while (trees<=10 || trees>=30);
- //Error checking for proper density amount
- do
- {
- System.out.print("Enter tree density: ");
- density=scan.nextDouble();
- } while (density<=.20 || density >=.80);
- scan.close();
- double [][] forest= new double [trees][trees];
- //fill the grid
- for (int row = 0; row < forest.length; row++) {
- for (int col = 0; col < forest[row].length; col++) {
- forest[row][col] = Math.random();
- }
- }
- System.out.println("");
- System.out.println("The original forest:");
- print2D(forest,density,treecount,trees);
- System.out.println("The final forest:");
- burn2D(forest,density,burncount,trees,burncount2);
- }
- public static void print2D (double [][] a,double d,int c,int t ) {
- //Prints the original forest
- for(int i = 0; i <a.length; i++) {
- for(int j = 0; j < a[i].length; j++) {
- if (a[i][j]<d) {
- System.out.print('T'+" ");
- c++;}
- else
- System.out.print('.'+" ");
- }
- System.out.println();
- }
- System.out.println("Percent populated: "+(double)c/Math.pow(t, 2));
- System.out.println("");
- }
- public static void burn2D (double[][] a,double d, int c, int t,int c2) {
- //Ignites the first row of 2D array on fire
- for(int q = 0; q <1; q++) {
- for(int p = 0; p < t; p++) {
- if (a[q][p]<d) {
- System.out.print('B'+" ");
- c2++;}
- else
- System.out.print('.'+" ");
- }
- System.out.println();
- //Problems are with this nested for loop
- for(int i = 1; i <a.length; i++) {
- for(int j = 0; j < a[i].length; j++) {
- if (a[i][j-1]=='B') //checks row above for 'B'
- System.out.print('B'+" ");
- else if (a[i+1][j-1]=='B') { //checks top right for 'B'
- System.out.print('B'+" ");
- }
- else if (a[i-1][j-1]=='B') { //checks top left for 'B'
- System.out.print('B'+" ");
- }
- else if (a[i][j]<d) {
- System.out.print('T'+" ");
- c++;}
- else
- System.out.print('.'+" ");
- }
- System.out.println();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement