View Single Post
ישן 15-09-06, 16:48   # 4
omaniusd
משתמש - היכל התהילה
 
מיני פרופיל
תאריך הצטרפות: Oct 2005
הודעות: 823

omaniusd לא מחובר  

ציטוט:
נכתב במקור על ידי Matchs
עשיתי בדיוק מה שאמרת, רק בלי ELSE - ועכשיו הכל פועל נהדר!
למי שרוצה לראות מחשבון בסיסי עם 2 מספרים ופעולות, בשפת סי-שארפ (אשמח לתגובות וכמובן ממך omaniusd):

PHP קוד:
using System;
using System.Collections.Generic;
using System.Text;

namespace Calculator
{
    class 
Program
    
{
        static 
void Main(string[] args)
        {
            
int A;
            
int B;
            
char C;

            
Console.WriteLine("Welcome to C# calculator!");
            
Console.WriteLine("Made by Rotem the programmer :P");

            
Console.WriteLine();

            
Console.Write("Please insert the first number: ");
            
int.Parse(Console.ReadLine());

            
Console.Write("Please insert the second number: ");
            
int.Parse(Console.ReadLine());

            
Console.Write("Please insert an action: ");
            
char.Parse(Console.ReadLine());

            if ((
== '/') || (== 0))
            {
                
Console.WriteLine();
                
Console.WriteLine("You can't division by zero!");
                
Console.Read();
            }

            else
            {
                switch (
C)
                {
                    case 
'+':
                        
Console.WriteLine("The Result: {0} {1} {2} = {3}"ACBB);
                        
Console.Read();
                        break;

                    case 
'-':
                        
Console.WriteLine("The Result: {0} {1} {2} = {3}"ACBB);
                        
Console.Read();
                        break;

                    case 
'*':
                        
Console.WriteLine("The Result: {0} {1} {2} = {3}"ACBB);
                        
Console.Read();
                        break;

                    case 
'/':
                        
Console.WriteLine("The Result: {0} {1} {2} = {3}"ACBB);
                        
Console.Read();
                        break;

                    default:
                        
Console.WriteLine("There is a problem! :(");
                        break;
                }
            }
        }
    }

אתה צריך לעשות שאם C הוא חילוק וגם B שווה אפס. בחיבור, זה לא מפריע לך.
עשית את ההמרה עם Parse. יש לזה את היתרונות והחסרונות שלו, בכזאת אפליקציה, זה לא רלוונטי.
שים לב שלפי זה (לא בדקתי בפועל, אבל נראה לי) אם אני מכניס B שהוא אפס וסימן חיבור, הוא עדיין יגיד שאינו יכול לחלק באפס (או שאני טועה).

הקוד כעיקרון יפה, רק תחליף את || ב &&
  Reply With Quote