الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كف أتمكن من وضع "witch Statment"في "method"جديد

بدأه Zaianb في 5 أبريل 2009 · 9 رد · 557 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

أخواني الكرام

الكود التالي عبارة عن آلة حسابية بسيطه

صيغة الكود كالتالي:

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace calc

{

class Program

{

static void Main(string[] args)

{

int n1;

int n2;

string op;

float answer;

Console.WriteLine("Welcome to my calculator");

Console.WriteLine("please Entar your frist number");

n1 = int.Parse(Console.ReadLine());

Console.WriteLine("please Entar the operation you want to use +,-,*,/");

op = Console.ReadLine();

Console.WriteLine("please Entar your second number");

n2 = int.Parse(Console.ReadLine());



answer = 0;

switch (op)

{

case "+":

answer = n1 + n2;

break;

case "-":

answer = n1 - 2;

break;

case "*":

answer = n1 * n2;

break;

case "/":

answer = n1 / n2;

break;

default: Console.WriteLine("sorry, this is uncorrect operation");

break;

}

Console.WriteLine(+ n1 + " " + op + " " + n2 + " = " + answer);

Console.ReadLine();

}

}

}

الكود يعمل بصورة جيده

لكني أرغب بوضع عملية

switch statment in new method

ولا أرغب باستخدام "return"

فكيف يمكنني ذلك

إذا كان هناك من لديه القدرة على مساعدتي فليتكرم علي بذلك

وسأكون له من الشاكرين

تحياتي لكم جميعا

#2

السلام عليكــم ورحمـة الله وبركاتــه ،،

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

namespace calc
{

	class Program
	{

		public static void Main(string[] args)
		{

			int n1;

			int n2;

			string op;

			float answer;

			Console.WriteLine("Welcome to my calculator");

			Console.WriteLine("please Entar your frist number");

			n1 = int.Parse(Console.ReadLine());

			Console.WriteLine("please Entar the operation you want to use +,-,*,/");

			op = Console.ReadLine();

			Console.WriteLine("please Entar your second number");

			n2 = int.Parse(Console.ReadLine());



			answer = 0;

			Compute(n1, n2, op, out answer);

			Console.WriteLine(+n1 + " " + op + " " + n2 + " = " + answer);

			Console.ReadLine();

		}

		private static void Compute(int n1, int n2, string op, out float answer)
		{
			answer = 0;
			switch (op)
			{

				case "+":

					answer = n1 + n2;

					break;

				case "-":

					answer = n1 - 2;

					break;

				case "*":

					answer = n1 * n2;

					break;

				case "/":

					answer = n1 / n2;

					break;

				default: Console.WriteLine("sorry, this is uncorrect operation");

					break;

			}

		}

	}

}

[وسط]لا اله الا انت سبحانك انى كنت من الظالمين

[/وسط]

#3

يعطيك العافيه أخوي

بس ليش أستخدمة

"Compute(n1, n2, op, out answer);"

ممكن توضح لي شنو وضيفتها

؟؟

مرة أخر أشكرك على الاستجابة وتقديم المساعدة

#4

اظن ان سؤالك لماذا استخدمت out في:

private static void Compute(int n1, int n2, string op, out float answer)

لأن out تعني التمرير بالمرجع ،

لماذا التمرير بالمرجع ؟ لأننا نريد ارجاع قيمة answer من Compute إلى Main (بدون استخدام return كما طلبت).

[وسط]لا اله الا انت سبحانك انى كنت من الظالمين

[/وسط]

#5

يعطيك العافيه أخي

أنا سويت كود من غير ما استخدم

return

بس استخدمت

ref

والكود يعمل بشكل صحيح

لكن عناك مشكله بسيطه

عندما تكون العملية

3

-

5

=

0

لمذا؟؟

يجب ان يكون

-2

صيغة الكود كالتالي

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace calc //Namespaces allow me to group related classes together

{

class Program //class is start now

{

public static void Main(string[] args) //static void Main method is the entry point for a Windows .NET console or desktop application

{ //main methos is start now

float n1; //To declare variable

float n2; //To declare  variable

string op; //To declare  variable

float answer; //To declare variable 



Console.WriteLine("Welcome to my calculator"); //use the Console.WriteLine() method to display the value of a variable to the screen

Console.WriteLine("please Entar your frist number");

n1 = float.Parse(Console.ReadLine()); //use the Console.ReadLine() method to read a value from the keyboard



Console.WriteLine("please Entar the operation you want to use +,-,*,/");

op = Console.ReadLine();

Console.WriteLine("please Entar your second number");

n2 = float.Parse(Console.ReadLine());





answer = 0;

Calc(ref n1, ref n2, op, ref answer);

Console.WriteLine(+n1 + " " + op + " " + n2 + " = " + answer);

Console.ReadLine(); //use to give you a time before the screen close

} //main method end

public static void Calc(ref float n1, ref float n2, string op, ref float answer)

{ //calc method start

answer = 0;

switch (op) //switch statement allows the program to select one of many options

{ //switch start

case "+":

answer = n1 + n2;

break; // //i use break because i don't want any subsequent cases to be executed after the first case

case "-":

while(n1>n2) //only do the sub if n1>n2 

answer = n1 - 2;

break;

case "*":

answer = n1 * n2;

break;

case "/":

answer = n1 / n2;

break;

default: Console.WriteLine("sorry, this is uncorrect operation"); ////i use default because i want to protect against invalid entries from the users 

break;

} //sitch method end

} //calc method end

}//class end

} //namespace end

أخي أعتذر على كثرة أسئلتي

اذا كنت لاتمانع الاجابة

فتفضل علي بها

وانا لك من الشاكرين

#6

لديك خطأ في الكود الذي كتبته:

case "-":

while(n1>n2) //only do the sub if n1>n2 

answer = n1 - 2;

عدل فيه بهذا الشكل:

case "-":

answer = n1 - n2;

[وسط]لا اله الا انت سبحانك انى كنت من الظالمين

[/وسط]

#7

لقد قمت بعمل ماطلبت مني

كانت العملية كالتالي

3

-

7

=

4

يجب ان يكون

-4

#8

هذا الكود كاملاً:

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




namespace calc //Namespaces allow me to group related classes together
{

	class Program //class is start now
	{

		public static void Main(string[] args) //static void Main method is the entry point for a Windows .NET console or desktop application
		{ //main methos is start now

			float n1; //To declare variable

			float n2; //To declare variable

			string op; //To declare variable

			float answer; //To declare variable 



			Console.WriteLine("Welcome to my calculator"); //use the Console.WriteLine() method to display the value of a variable to the screen

			Console.WriteLine("please Entar your frist number");

			n1 = float.Parse(Console.ReadLine()); //use the Console.ReadLine() method to read a value from the keyboard



			Console.WriteLine("please Entar the operation you want to use +,-,*,/");

			op = Console.ReadLine();

			Console.WriteLine("please Entar your second number");

			n2 = float.Parse(Console.ReadLine());
			answer = 0;
			Calc(ref n1, ref n2, op, ref answer);

			Console.WriteLine(n1 + " " + op + " " + n2 + " = " + answer);

			Console.ReadLine(); //use to give you a time before the screen close

		} //main method end

		public static void Calc(ref float n1, ref float n2, string op, ref float answer)
		{ //calc method start

			answer = 0;

			switch (op) //switch statement allows the program to select one of many options
			{ //switch start

				case "+":

					answer = n1 + n2;

					break; // //i use break because i don't want any subsequent cases to be executed after the first case

				case "-":
						answer = n1 - n2;

					break;

				case "*":

					answer = n1 * n2;

					break;

				case "/":

					answer = n1 / n2;

					break;

				default: Console.WriteLine("sorry, this is uncorrect operation"); ////i use default because i want to protect against invalid entries from the users 

					break;

			} //sitch method end

		} //calc method end

	}//class end

} //namespace end

[وسط]لا اله الا انت سبحانك انى كنت من الظالمين

[/وسط]

#9

أخي أشكرك كثير على المجهود

الله يعطيك العافيه

الكود أصبح يعمل بشكل جيد

لكن الى الان لم أعرف أين كان الخطأ!!

مره أخرى أشكرك

تحياتي لكم جميعا

#10

لقد تمكنت من أكتشاف الخطأ

case "-":



answer = n1 - 2;

يجب أن يكون

case "-":



answer = n1 - n2;

أشكركم جميعا

تحياتي لكم

مواضيع مشابهة