البرمجة

حل مشكلة تسلسل الأرقام بدون ثم

To solve this problem, you can check if the current number is the last number in the sequence (13 in this case) and then decide whether to print “then” or not. Here’s how you can modify your code:

csharp
class Program { static void Main(string[] args) { int start; for (start = 4; start <= 13; start++) { Console.Write(start); if (start < 13) { Console.Write(" then "); } } Console.ReadKey(); } }

In this modified code, the if statement inside the for loop checks if the current number is less than 13. If it is, it prints ” then ” after the number. If the current number is 13, it skips printing ” then ” to avoid having it appear after the last number.

المزيد من المعلومات

لحل هذه المشكلة، يمكنك استخدام متغير لتتبع ما إذا كان الرقم الحالي هو الرقم الأخير في التسلسل (13 في هذه الحالة)، ومن ثم تقرر ما إذا كان يجب طباعة “ثم” أم لا. إليك كيف يمكنك تعديل الكود:

csharp
class Program { static void Main(string[] args) { int start; for (start = 4; start <= 13; start++) { Console.Write(start); if (start < 13) { Console.Write(" ثم "); } } Console.ReadKey(); } }

في هذا الكود المعدل، تقوم العبارة if داخل حلقة for بالتحقق مما إذا كان الرقم الحالي أقل من 13. إذا كان كذلك، فإنها تقوم بطباعة ” ثم ” بعد الرقم. إذا كان الرقم الحالي هو 13، فإنها تتجاوز طباعة ” ثم ” لتجنب ظهورها بعد الرقم الأخير.

زر الذهاب إلى الأعلى