Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي العاشر - حل التمرين الخامس بلغة جافا

المطلوب

أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد بين 1 و 9 يمثل عدد أسطر الشكل الذي سيتم رسمه.


الحل بلغة جافا

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 the number of lines: ");
n = input.nextInt();
}
while (n < 1 || n > 9);
for (int i = 1; i <= n; i++)
{
for (int j = n; j >= i; j--)
{
if (i == 1 || j == n || j == i)
{
System.out.print(j + " ");
}
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 the number of lines: "); n = input.nextInt(); } while (n < 1 || n > 9); for (int i = 1; i <= n; i++) { for (int j = n; j >= i; j--) { if (i == 1 || j == n || j == i) { System.out.print(j + " "); } else { System.out.print(" "); } } System.out.println(); } } }

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 6 عند التشغيل.

Enter the number of lines: 6
6 5 4 3 2 1 
6       2 
6     3 
6   4 
6 5 
6 
		

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة