لم أفهم طريقة حل التمرين رقم 2 في التحدي السادس في دورة الخوارزميات و هياكل البيانات
فكرة هذا التحدي هو الطلب من المستخدم إدخال عدد الأسطر و على حسب العدد الذي يدخله يتم رسم الشكل التالي:
في الموقع يوجد حلّين للتمرين، الحل التالي هو الحل المختصر و هو الذي لم أستطع فهمه.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
do {
System.out.print("Enter an odd number as the number of lines: ");
n = input.nextInt();
}
while (n <= 0 || n%2 == 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if ((i <= n / 2 + 1 && j >= i && j <= n - i + 1) || (i > n / 2 - 1 && j <= i && j > n - i))
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
do {
System.out.print("Enter an odd number as the number of lines: ");
n = input.nextInt();
}
while (n <= 0 || n%2 == 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if ((i <= n / 2 + 1 && j >= i && j <= n - i + 1) || (i > n / 2 - 1 && j <= i && j > n - i))
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}