הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   צריך עזרה בתרגיל בC# (https://hosts.co.il/forums/showthread.php?t=59339)

ohadsti 25-01-08 19:17

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

תוצאה של שחקן במשחקון טניס יכולה להיות אחת מתוך הערכים הבאים : 0,15,30,40,60
נקודות. נקוד 60 הוא ניצחון והמישחקון מסתיים בניצחון של אחד השחקנים. פתור ב שפת C# . המקבל כקלט תוצאת משחקון בין שתי שחקנים. אם מספר הנקודות שצבר אחד השחקנים אינו חוקי, יש להדפיס את ההודעה " לא חוקי" אחרת יש להדפיס את הפרש הנקודות בין שני השחקנים.

תודה מראש לעוזר.

נ.ב הנה משהו שניסית לעשות (לא לצחוק עלי)
PHP קוד:

int xy;
            
int.Parse(Console.ReadLine());
            
int.Parse(Console.ReadLine());
           if (
x>y)
               switch (
y)
               {
                   case 
0Console.WriteLine("60");
                   case 
15Console.WriteLine("45");
                   case 
30Console.WriteLine("30");
                   case 
40Console.WriteLine("20");
                   case 
60Console.WriteLine("0");
                   
defavltConsole.WriteLine("error");
               }
                   
                else

                switch (
x)
            {
                
case0Console.WriteLine("60");
                    case  
5Console.WriteLine("45");
                    case 
30Console.WriteLine("30");
                    case 
40Console.WriteLine("20");
                   case 
60Console.WriteLine("0");
                          
defavltConsole.WriteLine("error"); 


mlnn 25-01-08 19:32

תנסה משהו כזה
קוד:

int x, y;
            x = int.Parse(Console.ReadLine());
            y = int.Parse(Console.ReadLine());

if((x>60) || (y>60) || ((x==y) && (x==60)))
        Console.WriteLine("error");
else
        Console.WriteLine(Math.Abs(x-y));


דור 25-01-08 19:59

לא מבין מה התוכנית אמורה לעשות :| לא מבין מה כתבת

ohadsti 25-01-08 20:21

ציטוט:

נכתב במקור על ידי mlnn (פרסם 603427)
תנסה משהו כזה
קוד:

int x, y;
            x = int.Parse(Console.ReadLine());
            y = int.Parse(Console.ReadLine());

if((x>60) || (y>60) || ((x==y) && (x==60)))
        Console.WriteLine("error");
else
        Console.WriteLine(Math.Abs(x-y));


אחי זה נחמד אבל אם אני רושם נגיד שהתוצאה היא 17 ו23 זה לא רושם לי שזה טעות אלה מחשב.

Daniel 25-01-08 21:08

defavlt: Console.WriteLine("error");
default

mayden 25-01-08 21:40

אז תעשה:
קוד:

if((x != 40) || ( x != 30) || ( x != 15) || ( x != 0) || ( x != 60) || (y != 40) || ( y != 30) || (y != 15) || ( y != 0) || (y != 60))
Console.WriteLine("Illegal..");
else {
Console.WriteLine(Math.Abs(x-y));


ohadsti 25-01-08 22:04

ציטוט:

נכתב במקור על ידי mayden (פרסם 603477)
אז תעשה:
קוד:

if((x != 40) || ( x != 30) || ( x != 15) || ( x != 0) || ( x != 60) || (y != 40) || ( y != 30) || (y != 15) || ( y != 0) || (y != 60))
Console.WriteLine("Illegal..");
else {
Console.WriteLine(Math.Abs(x-y));


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

aviho1 25-01-08 22:44

קוד:

if((x != 40) || ( x != 30) || ( x != 15) || ( x != 0) || ( x != 60) || (y != 40) || ( y != 30) || (y != 15) || ( y != 0) || (y != 60))
Console.WriteLine("Illegal..");
else {
if(x>y)
Console.WriteLine(x-y);
else
Console.WriteLine(y-x);

תיקון קטן

mlnn 25-01-08 23:10

אתה לא רוצה לנסות לבד? הרי הבנת מה לעשות לא?
וכנראה שswitch באמת יהיה יותר טוב פה. הקוד המקורי שלך דיי בסדר, רק כמו שדניאל אמר, כותבים default. (וגם תסגור את הswitch ותתקן את הcase0).


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

ohadsti 26-01-08 09:52

אוקי עם התוכנית הזאת הצלחתי להגיע שזה עושה את ההפרש אבל אם אני מכניס מספרים כמו 31 ו60 זה מחשב את ההפרש במקום לכתוב . error

קוד:

  {
int x, y;
            x = int.Parse(Console.ReadLine());
            y = int.Parse(Console.ReadLine());
          if (x>y)
              switch (x)
              {
                  case 0 :
                      Console.WriteLine(y-x); break;
                  case 15:
                      Console.WriteLine(y-x); break;
                  case 30:
                      Console.WriteLine(y-x); break;
                  case 40:
                      Console.WriteLine(y-x); break;
                  case 60:
                      Console.WriteLine(x-y); break;
                  default:
                      Console.WriteLine("error"); break;
              }
                   
                else

              switch (y)
              {
                  case 0:
                      Console.WriteLine(x-y); break;
                  case 15:
                      Console.WriteLine(x-y); break;
                  case 30:
                      Console.WriteLine(x-y); break;
                  case 40:
                      Console.WriteLine(x-y); break;
                  case 60:
                      Console.WriteLine(y-x); break;
                  default:
                      Console.WriteLine("error"); break;
              }
        }
    }
}



כל הזמנים הם GMT +2. הזמן כעת הוא 00:35.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ