1. Using a for loop to count the number of entered lines

Let's write a program that reads 10 lines from the keyboard and displays the number of lines that were numbers. Example:

Code Explanation
Scanner console = new Scanner(System.in);
int count = 0;
for (int i = 0; i < 10; i++)
{
   if (console.hasNextInt())
      count++;
   console.nextLine();
}
System.out.println(count);
Create a Scanner object to read data from the console.
Store the number of numbers in the count variable.
Loop from 0 to 10 (not including 10).

If a number is entered,
then increase count by one.
Read a line from the console and don't save it anywhere.

Display the calculated count on the screen.
Note

If the line contains multiple tokens separated by spaces, and the first of them is a number, then the hasNextInt() method will return true, even if the other tokens are not numbers. That means that our program will work correctly only if just one token is entered on each line.


2. Calculating the factorial using a for loop

Let's write a program that doesn't read in anything, but instead calculates something. Something difficult. For example, the factorial of the number 10.

The factorial of a number n (denoted by n!) is the product of a series of numbers: 1*2*3*4*5*..*n;

Code Explanation
int f = 1;
for (int i = 1; i <= 10; i++)
   f = f * i;
System.out.println(f);
We store the product of numbers in the f variable.
Loop from  to  (inclusive).
Multiply  by the next number (save the result in ).
Display the calculated amount on the screen.110ff

The starting value is f = 1, because we are multiplying f by the numbers. If f were originally 0, then the product of all the numbers multiplied by 0 would be 0.


3. Using a for loop to draw on the screen

Let's write a program that draws a triangle on the screen. The first line consists of 10 asterisks, the second — 9 asterisks, then 8, etc.

Code Explanation
for (int i = 0; i < 10; i++)
{
   int starCount = 10 - i;
   for (int j = 0; j < starCount; j++)
      System.out.print("*");
   System.out.println();
}
Loop through the lines (there should be 10 lines in total).

Calculate how many asterisks should be in the line.
Loop over the individual asterisks
(display starCount asterisks).
Move the cursor to the next line so the lines are separate.

We need to have two nested loops here: the inner loop is responsible for displaying the correct number of asterisks on a given line.

And the outer loop is needed to loop through the lines.


6
Task
New Java Syntax,  level 6lesson 4
Locked
One large array and two small ones
1. Create an array of 20 numbers. 2. Populate it with values from the keyboard. 3. Create two arrays of 10 numbers each. 4. Copy the large array into the two small ones: half the numbers into the first small array, and the second half into the second small array. 5. Display the second small array, e