עבור לתוכן
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

פורסם

אני מנסה לכתוב תוכנית שבה אני אכניס טקסט מסויים שיהיה מורכב ממילים, אם יש תווים כמו פסיק, נק', דולר או רווח אז זה אומר שמה שהיה עד עכשיו זו מילה ואז אני מתחיל מחדש לחפש את המילה הבאה ובסוף להציג את כל המילים בנפרד.

עכשיו התוכנית תתחיל לעבוד אחרי שאני מכניס טקסט ולוחץ פעמיים אנטר ז"א, אני יכול להכניס טקסט מסויים ואז באנטר להתחיל להכניס עוד טקסט בשורה חדשה אבל אם אני נמצא בשורה חדשה ולא מכניס כלום אלא לוחץ אנטר אז אני מקבל את המילים.

לדוגמא אם אני מכניס את הטקסט:

test abcd$rrr.tyu ver

asd dfg

אז אני מקבל את המילים מסודרות ככה:

test

abcd

rrr

tyu

ver

asd

dfg

התחלתי לכתוב משהו אבל לא הולך לי, אני לא יודע מה לשים ב-ELSE, הוא לא נכנס לי טוב ללולאה, משהו שם אני עושה לא טוב:


[i][i][i][i][i][i]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace PrintWordWordFromText
{
class Program
{
static void Main(string[] args)
{
//read text from user
Console.Write("Enter Text: ");
string line = Console.ReadLine();


//define array
string[] Words = new string[100];


//define temp
string temp = "";


int wordsIndex = 0;


for (int i = 0; i < line.Length; i++)
{
//We have a regular character - part of a word
if (line[i] != ' ' || line[i] != ',' || line[i] != '.' || line[i] != '$')
{
temp += line[i];
}


//special characters
else
{
Words[wordsIndex] = temp;
wordsIndex++;
temp = "";
}
}


for (int i = 0; i < line.Length; i++)
{
Console.Write(Words[i]);
}

Console.ReadLine();


}
}
}

פורסם

באמת היה לי רצון לעזור לך, אבל כתבת כך כך לא ברור אז עזבתי.

תוסיף קצת סימני ניקוד ואולי זה קצת יותר ברור

כמו כן לא ציינת מה הבעיה עם הקוד שלך

פורסם

ה-IF בלולאת FOR הראשונה לא נכון

אני מנחש שזה תרגיל לימודי אז תנסה לחשוב לבד.

אגב המתודה string.split עושה את העבודה אבל שוב זה בטח תרגיל.

בנוסף כדאי להשתמש ב StringBuilder במקום string רגיל במשתנה temp - כי string רגיל הוא immunable, ז"א שבכל פעם שאתה משנה אותו אז נוצר string חדש בהקצאת זכרון חדשה ובלולאה גדולה אתה מקצה ומשחרר המון זכרון סתם.

פורסם

אתה לא עושה כלום עם הREADLINE השני?

פורסם
  • מחבר

אני יודע שיש לי בעיה ב-IF הראשון אבל אני לא מוצא למה

פורסם
  • מחבר

אם אני מכניס לדוגנא את הטקסט: ab c

a ואח"כ b נכנסים למשתנה temp ואז כשמגיע תו ריק הוא גם נכנס ל-if ולא ל-else ואני לא מבין למה כי הוא הרי שווה למה שרשום בתנאי.

פורסם

ערכתי את הפיתרון שלך שיתאים לדרישות התרגיל. נסה לפתור בעצמך ותעזר בפיתרון במידה ולא תצליח.

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


namespace PrintWordWordFromText
{
class Program
{
static void Main(string[] args)
{
do
{
//read text from user
String line = "", eof ="", temp = "";
Console.Write("Enter Text: ");
while (true)
if ((eof = Console.ReadLine()) != "")
line += eof + " ";
else
break;


string[] Words = new string[100];
int wordsIndex = 0;

for (int i = 0; i < line.Length; i++)
{
//We have a regular Character - part of a word


if (line[i] != ' ' & line[i] != ',' & line[i] != '.' & line[i] != '$')
temp += line[i];
else if (temp != "")
{
Words[wordsIndex] = temp;
wordsIndex++;
temp = "";
}
}

Console.WriteLine("You have enterd the following words:");
for (int i = 0; i < wordsIndex; i++)
Console.WriteLine(Words[i]);

} while (true);

}
}
}

פורסם

אם אני מכניס לדוגנא את הטקסט: ab c

a ואח"כ b נכנסים למשתנה temp ואז כשמגיע תו ריק הוא גם נכנס ל-if ולא ל-else ואני לא מבין למה כי הוא הרי שווה למה שרשום בתנאי.

זה קורה בגלל שהשתמשת באופרטור הלוגי OR במקום AND.

פורסם
  • מחבר

nat64x:

לא ממש הבנתי את הפיתרון שלך אז עשיתי משהו אחר:

הבעיה שלי עכשיו היא שאם יש לי למשל ab c.

a ואח"כ b נכנסים ל-temp ולמערך אבל ה-c שבא אחרי הרווח נכנס ל-temp אבל ה-temp לא מעדכן את המערך וככה בסוף מוצג לי רק ab.



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


namespace PrintWordWordFromText
{
class Program
{
static void Main(string[] args)
{
//read text from user
Console.Write("Enter Text: ");
string line = Console.ReadLine();


//define array
string[] Words = new string[100];


//define temp
string temp = "";


int WordIndex = 0;


for (int i = 0; i < line.Length; i++)
{
//We have a regular character - part of a word
if (line[i] != ' ' && line[i] != ' ' && line[i] != ',' && line[i] != '.' && line[i] != '$')
{
temp += line[i];
}


//special characters
else
{
Words[WordIndex] = temp;
WordIndex++;
temp = "";
}
}


for (int i = 0; i < WordIndex; i++)
{
Console.Write(Words[i]);
}

Console.ReadLine();


}
}
}

פורסם
  • מחבר

ניסיתי כיוון טיפה שונה, אני יכול לרשום שאם האורך של השורה הוא אפס אז צא מהלולאה ?

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



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


namespace PrintWordWordFromText
{
class Program
{
static void Main(string[] args)
{
//define temp
string temp = "";


int WordIndex = 0;

//read text from user
Console.Write("Enter Text: ");
string line = Console.ReadLine();


//define array
string[] Words = new string[100];


while (line.Length != 0)
{
for (int i = 0; i < line.Length; i++)
{
//We have a regular character - part of a word
if (line[i] != ' ' & line[i] != ' ' & line[i] != ',' & line[i] != '.' & line[i] != '$')
{
temp += line[i];
}


//special characters
else
{
Words[WordIndex] = temp;
WordIndex++;
temp = "";
}
}
}


for (int i = 0; i < WordIndex; i++)
{
Console.Write(Words[i]);
}

Console.ReadLine();


}
}
}

פורסם

nat64x:

לא ממש הבנתי את הפיתרון שלך אז עשיתי משהו אחר:

הבעיה שלי עכשיו היא שאם יש לי למשל ab c.

a ואח"כ b נכנסים ל-temp ולמערך אבל ה-c שבא אחרי הרווח נכנס ל-temp אבל ה-temp לא מעדכן את המערך וככה בסוף מוצג לי רק ab.

הפיתרון שלי זה בעצם הפיתרון שלך רק עם תיקון באגים. עם משהו לא ברור אשמח לעזור. אני ממליץ לך לעבור באמצעות דיבאגר על הקוד שורה אחר שורה כדי להבין אותו יותר לעומק. רוב השאלות שלך יכולים לענות בצורה הזאת די בקלות.

ה- C לא מוצג, כי אחרי שהוא נכנס ל- IF הראשון ומוסף אל ה - TEMP באיטרציה הבאה של הלולאה הוא כבר הגיע לסוף הסטרינג. ולכן משפט ה - ELSE שמכניס אותו אל תוך מערך הסטרינ לעולם לא יתבצע. פתרון פשוט שמצאתי לכך הוא שרשור של התו " " לסוף הסטרינג לפני הכניסה ללולאה.

ניסיתי כיוון טיפה שונה, אני יכול לרשום שאם האורך של השורה הוא אפס אז צא מהלולאה ?

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

זה בדיוק מה שהקוד הבא עושה: (הוא גם משרשר את התו " " לאחר כול אנטר כדי להפריד בין המילים)

       String line = "", eof ="", temp = ""; 
Console.Write("Enter Text: ");
while (true)
if ((eof = Console.ReadLine()) != "")
line += eof + " ";
else
break;

פורסם
  • מחבר

לבנתיים התקדמתי קצת (עדיין יש בעיות) אבל אני אסתכל על מה שעשית ואנסה ללמוד.

תודה רבה !

פורסם
  • מחבר

יש לי את אותה הבעיה שהלולאה לא נכנסת ל-ELSE, לא ממש הבנתי איך מימשת את הטריק עם התו הנוסף, אתה יכול להסביר לי ?

זה הקןד שלי כרגע:



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


namespace PrintWordWordFromText
{
class Program
{
static void Main(string[] args)
{
//read text from user
Console.Write("Enter Text: ");
string line = Console.ReadLine();


//define array
string[] Words = new string[100];


//define temp
string temp = "";


int WordIndex = 0;


while (line.Length != 0)
{
for (int i = 0; i < line.Length; i++)
{
//We have a regular character - part of a word
if (line[i] != ' ' & line[i] != ' ' & line[i] != ',' & line[i] != '.' & line[i] != '$')
{
temp += line[i];
}
//special characters
else
{
Words[WordIndex] = temp;
WordIndex++;
temp = "";
}
}


line = Console.ReadLine();
}


for (int i = 0; i < WordIndex; i++)
{
Console.Write(Words[i]);
}


Console.ReadLine();


}
}
}

פורסם

תכניס את השורה הבאה לפני לולאת ה WHILE

l

line += line + " ";

בלי להכניס את השורה הזאת הקוד שלך יצא נכון רק אם תכניס בקלט את הערך " ab c". כלומר רווח לאחר ה - C ואז אנטר.

הקלט תמיד יהיה צריך להסתיים באחר מהתווים שציינת במשפט ה IF כדי שהקוד שכתבת יהיה תקין.

if (line[i] != ' ' & line[i] != ' ' & line[i] != ',' & line[i] != '.' & line[i] != '$')

פורסם
  • מחבר

רגע זה לא טוב זה בעצם מכפיל לי את כל מה שאני מכניס

אני אני קולט a זה מדפיס לי אותו פעמיים.

ארכיון

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

דיונים חדשים

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.