עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

עזרה ב-C# - סדרה חשבונית

Featured Replies

פורסם

יש לי תרגיל שאני אמור לקלוט בו N איברים,A1 ו-d ולהציג את ה-N האיברים של הסדרה ואת סכומה.

כתבתי תוכנית וחילקתי אותו ל-3:

כשה-N גדול מ-2

כשה-N קטן מ-2 (שווה 1)

כשה-N שווה 2

באופציה הראשונה כשה-N גדול מ-2 הכל בסדר וזה עובד לי אבל איך שאני מווסיף את 2 האופציות האחרונות משהו מתחרבש לי:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace T100_2
{
class Program
{
static void Main(string[] args)
{


int count = 0;
int sum = 0;
Console.Write("Enter N: ");
string stN = Console.ReadLine();
int N = int.Parse(stN);


Console.Write("Enter A1: ");
string stA1 = Console.ReadLine();
int A1 = int.Parse(stA1);


Console.Write("Enter d: ");
string std = Console.ReadLine();
int d = int.Parse(std);


while (N>2 && count<N)
{
Console.WriteLine(A1);
sum = sum + A1;
A1 = A1 + d;
count++;
}


Console.WriteLine("The Sum Of The Series Is: " + sum);
Console.ReadLine();


if (N==2)
{
Console.WriteLine(A1);
Console.WriteLine(A1 + d);
Console.WriteLine("The Sum Of The Series Is: " + (A1 + A1 + d));
}


else
{
Console.WriteLine(A1);
Console.WriteLine("The Sum Of The Series Is: " + A1);
}


}
}
}

פורסם

שים לב שאתה עושה את הקטע הבא אחרי הלולאה לכל N:

Console.WriteLine("The Sum Of The Series Is: " + sum);
Console.ReadLine();

כלומר גם אם N קטן או שווה 2 אתה תדפיס את השורה הזאת שם עם sum=0, ואז עוד פעם בתוך אחד משני התנאים הבאים. בגלל זה יש לך בלאגן. מעבר לזה הייתי ממליץ לעשות לולאה אחת שמטפלת בכל N - הבעיה הזו ממש לא דורשת if בשביל פתרון. לולאה כללית לכל N תטפל בזה היטב ותפשיט את כל העניין.

פורסם

א. לא ברור למה בכלל אתה מפריד למקרים.

ב. מה המשמעות של השורה הזו:

while (N>2 && count<N)

אם בתוך הלולאה אתה בכלל לא משנה את N? אין טעם לשים ב-while תנאי שאף פעם לא ישתנה. אם אתה רוצה שהלולאה תרוץ רק אם N>2 אז תשתמש ב-if מחוץ לה.

פורסם
  • מחבר

איזה לולאה אני יכול לעשות שמטפלת בכל N ?

פורסם

...בדיוק אותה לולאה שעשית? (רק בלי להגביל N>2 כמובן)

חוץ מזה, למדת לולאת for?

פורסם
  • מחבר

כן למדתי FOR

זה יהיה בסדר ? (אין לי את הסביבת עבודה פה אז אני לא יכול לבדוק)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace T100_2
{
class Program
{
static void Main(string[] args)
{




int count = 0;
int sum = 0;
Console.Write("Enter N: ");
String stN = Console.ReadLine();
int N = int.Parse(stN);




Console.Write("Enter A1: ");
String stA1 = Console.ReadLine();
int A1 = int.Parse(stA1);




Console.Write("Enter d: ");
String std = Console.ReadLine();
int d = int.Parse(std);




for (count<N)
{
Console.WriteLine(A1);
sum = sum + A1;
A1 = A1 + d;
count++;
}


Console.WriteLine("The Sum Of The Series Is: " + sum);




}
}
}

פורסם

לא השתמשת ב-for בצורה נכונה - פשוט החלפת את המילה while ב-for (אם זה היה while אז זה היה בסדר דווקא).

פורסם
  • מחבר

אתה אומר שאם הייתי שם בתוכנית האחרונה פה WHILE במקום FOR זה היה בסדר ?

אתה יכול להסביר לי מה ההבדלים ?

פורסם

כן, זה מה שאני אומר.

אתה מוזמן לפתוח את הספר/אתר/מורה שאתה לומד ממנו ולראות מה ההבדל בין for ל-while, ואיך ומתי משתמשים בכל אחד מהם.

פורסם
  • מחבר

אוקיי תודה רבה

אגב אני מחפש (זה כבר לא לי) ספר טוב וממצה (אפילו באנגלית כי אני יודע שבעברית הם לא משהו) לשפת C# ואם אפשר גם אז שיהיה אפשר להשיג אותו בארץ

תודה.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.