In: Computer Science
in java pls
Write a program for spiritual lumberjacks who want to show their appreciation for each tree they 'kill' by celebrating its years of life.
Ask the lumberjack how many trees he wants to cut. Verify you got the number correctly by printing it to output.
Write a loop that iterates over all the trees the lumberjack wants to cut down. Print the tree number to the screen to make sure your loop is working correctly.
Query the lumber jack for how many rings are in this tree and print this number to make sure you got the correct number of rings.
Write an inner loop that iterates over the years of the tree. Print each year to make sure you are iterating correctly and hitting each year that it lived.
Change the necessary print statements and to match the correct formatting from the output examples.
input: 2 3 4
expected output:
How many trees do you want to cut down?
2
0
How many rings are in tree 0?
3
We humbly respect tree number 0 and celebrate its 1 birthday.
We humbly respect tree number 0 and celebrate its 2 birthday.
We humbly respect tree number 0 and celebrate its 3 birthday. 1
How many rings are in tree 1?
4
We humbly respect tree number 1 and celebrate its 1 birthday.
We humbly respect tree number 1 and celebrate its 2 birthday.
We humbly respect tree number 1 and celebrate its 3 birthday.
We humbly respect tree number 1 and celebrate its 4 birthday.
Here is the code for the following pproblem with proper commenting so that you an easily understand the flow of the code. however if you feel any problem please feel free to ask.
-----------------------------------------------------------------------------------------------------------
Code:
import java.util.Scanner;
public class Main
{
//execution of program start from here
public static void main(String[] args) {
//creating Scanner class object to
take user input
Scanner scnr = new
Scanner(System.in);
//ask Number of trees user wants
tot cut down
System.out.println("How many trees
you want to cut down?");
int trees = scnr.nextInt() ;
//loop throght all trees
for(int i=0; i<trees; i++)
{
System.out.println(i);
System.out.println("How many rings
are in tree " + i + "?");
int rings = scnr.nextInt();
//celebrate all birthday of
tree
for(int j=0; j<rings; j++)
{
System.out.println("We humbly
respect tree number " + i + " and celebrate its " + (j+1) + "
birthday.");
}
}
}
}
---------------------------------------------------------------------------------------------------------------------
Output: