עבור לתוכן

עוד שאלה בC#

Featured Replies

פורסם

אני יושב עליה כבר יותר משעה. ניסתי לפתור לבד ולא הצלחתי

זה מה שהצלחתי:

      int num_of_players;
int rolling;
int zover_while=0;

char stoping;

Console.Write("Enter the number of the playeres: ");
num_of_players = int.Parse(Console.ReadLine());
string[] color = new string[num_of_players];
for (int i = 0; i < num_of_players; i++)
{
Console.Write("Player number {0} enter color: ", i + 1);
color[i] = (Console.ReadLine());
}//for
stoping=char.Parse (Console.ReadLine());
while (stoping != 'Q')
{
zover_while++;
stoping = char.Parse(Console.ReadLine());
}
int[] roll = new int[zover_while];
for(int i=0;i<zover_while;i++)
{
Random rnd = new Random();
rolling= rnd.Next(1, num_of_players + 1);
roll[rolling]++;
}

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

אשמח לעזרה ואם אפשר גם הסבר זה בכלל מבורך

לילה טוב ותודה מראש :xyxthumbs:

[attachment deleted by admin]

פורסם

הכרזת על האובייקט rnd כל פעם מחדש, זו טעות, הפתרון שלך דיי בסדר, רק כמה שינויים מרכזיים:

int num_of_players;
int rolling;
int zover_while=0;
int maxPosition; //יכיל את האינדקס של הצבע שהוגרל הכי הרבה פעמים
Random rnd = new Random();
char stoping;

Console.Write("Enter the number of the playeres: ");
num_of_players = int.Parse(Console.ReadLine());
int[] roll = new int[num_of_players];
string[] color = new string[num_of_players];
for (int i = 0; i < num_of_players; i++)
{
roll[i] = 0; //הוספת איפוס
Console.Write("Player number {0} enter color: ", i + 1);
color[i] = (Console.ReadLine());
}//for
stoping=char.Parse (Console.ReadLine());
while (stoping != 'Q')
{
rolling = rnd.Next(0, num_of_players - 1);
roll[rolling]++; //הגדלת מונה ההגרלות של אותו צבע
stoping = char.Parse(Console.ReadLine());
}
maxPosition = 0;
for(i = 1; i < num_of_players; i++)
{
if(roll[i] > roll[maxPosition])
maxPosition = i;
}
Console.WriteLine("The Winning Color is " + color[maxPosition]);

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

פורסם
  • מחבר

תודה רבה Hardstyler !

עשיתי משהו ממש דומה למה שאתה רשמת בסוף אבל אצלי זה לא פעל :kopfpatsch:

פורסם

על לא דבר, אל תדאג, הראש שלך במקום הנכון :P

פורסם

Hardstyler אתה יכול לנסות לעזור גם לי קצת? בבקשה.

אל תגנוב ת'רדים של אחרים.

ארכיון

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

דיונים חדשים