1. Nggunakake fordaur ulang kanggo count nomer baris ngetik

Ayo nulis program sing maca 10garis saka keyboard lan nuduhake nomer baris sing nomer. Tuladha:

Kode Panjelasan
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.
Cathetan

Yen baris ngemot pirang-pirang token sing dipisahake dening spasi, lan sing pisanan yaiku nomer, banjur hasNextInt()cara kasebut bakal bali true, sanajan token liyane ora nomer. Tegese program kita bakal bisa digunakake kanthi bener yen mung siji token sing dilebokake ing saben baris.


2. Ngitung faktorial nggunakake forloop

Ayo nulis program sing ora maca apa-apa, nanging ngetung soko. Soko angel. Contone, faktorial saka nomer 10.

Faktorial saka nomer n(dilambangake dening n!) minangka produk saka seri nomer: 1*2*3*4*5*..*n;

Kode Panjelasan
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 1 to 10 (inclusive).
Multiply f by the next number (save the result in f).
Display the calculated amount on the screen.

Nilai wiwitan punika f = 1, amarga kita multiply fdening nomer. Yen fpadha asline 0, banjur prodhuk kabeh nomer pingan dening 0bakal 0.


3. Nggunakake fordaur ulang kanggo tarik ing layar

Ayo nulis program sing nggambar segitiga ing layar. Baris pisanan kasusun saka 10asterisk, kaloro - 9asterisks, banjur 8, etc.

Kode Panjelasan
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.

Kita kudu duwe loro puteran nested kene: daur ulang utama tanggung jawab kanggo nampilake nomer bener saka asterisk ing baris tartamtu.

Lan daur ulang njaba perlu kanggo daur ulang liwat garis.