728x90
1. Square() ๋ฉ์๋๋ฅผ ๊ตฌํํด ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์. Square() ํจ์๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๊ณฑํ์ฌ ๋ฐํํฉ๋๋ค.
using System;
namespace Chapter06Practice
class MainApp
{
static double Square(double x)
{
return x * x;
}
static void Main(string[] args)
{
Console.Write("์๋ฅผ ์
๋ ฅํ์ธ์: ");
string str = Console.ReadLine();
if (!double.TryParse(str, out double d) && !int.TryParse(str, out int i))
{
Console.WriteLine("์ ์ ํน์ ์ค์๋ง ์
๋ ฅํด์ฃผ์ธ์");
}
else
{
double x = Convert.ToDouble(str);
Console.WriteLine("๊ฒฐ๊ณผ : {0}", Square(x));
}
}
}
}
2. ๋ค์ ์ฝ๋์์ Main() ๋ฉ์๋๋ฅผ ์คํํ ํ์ mean์ ์ผ๋ง์ ๊ฐ์ ๊ฐ์ง๊น์? ๊ทธ ๊ฐ์ ๊ฐ์ง๊ฒ ๋๋ ์์ธ์ ๋งํ๊ณ ์ฝ๋๋ฅผ ๊ณ ์น์ธ์.
public static void Main(string[] args)
{
double mean = 0;
Mean(1, 2, 3, 4, 5, mean);
Console.WriteLine($"mean is {mean}");
}
public static void Mean(double a, double b, double c, double d, double e, double mean)
{
mean = (a + b + c + d + e) / 5;
}
์ ์ฝ๋๋ mean์ ์ฐธ์กฐํด์ ๊ฐ์ ๋ณ๊ฒฝ์์ผ์ผํ๋ค. ์์ ๋ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
public static void Main(string[] args)
{
Mean(1, 2, 3, 4, 5, out double mean);
Console.WriteLine($"mean is {mean}");
}
public static void Mean(double a, double b, double c, double d, double e, out double mean)
{
mean = (a + b + c + d + e) / 5;
}
3. ๋ค์ ์ฝ๋์์ Plus() ๋ฉ์๋๊ฐ double ํ ๋งค๊ฐ๋ณ์๋ฅผ ์ง์ํ๋๋ก ์ค๋ฒ๋ก๋ฉํ์ธ์. ์ด ํ๋ก๊ทธ๋จ์ด ์์ฑ๋ ํ์ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์์ผ ํฉ๋๋ค.
public static void Main(string[] args)
{
int a = 3; int b = 4;
Plus(a, b, out int c);
Console.WriteLine("a + b = {0}", c);
}
public static void Plus(int a, int b, out int c)
{
c = a + b;
}
์ค๋ฒ๋ก๋ฉ ํ ์ฝ๋
public static void Main(string[] args)
{
int a = 3; int b = 4;
Plus(a, b, out int c);
Console.WriteLine("a + b = {0}", c);
double a1 = 3.1; double b1 = 4.34;
Plus(a1, b1, out double c1);
Console.WriteLine("a1 + b1 = {0}", c1);
}
public static void Plus(int a, int b, out int c)
{
c = a + b;
}
public static void Plus(double a, double b, out double c)
{
c = a + b;
}
728x90
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ๋ถํ ํด๋์ค, ํ์ฅ ๋ฉ์๋ (0) | 2023.08.17 |
---|---|
[C#] ์์, ๋คํ์ฑ, ์ค๋ฒ๋ผ์ด๋ฉ (0) | 2023.08.17 |
[C#] ์ฐธ์กฐ ๋งค๊ฐ๋ณ์ ์ ๋ฌ, ์ฌ๋ฌ ๊ฐ์ ๊ฐreturn, ๊ฐ๋ณ ๊ฐ์์ ์ธ์, ์ ํ์ ์ธ์ (0) | 2023.08.16 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter05: ์ฐ์ต๋ฌธ์ (0) | 2023.08.16 |
[C#] switch๋ฌธ๊ณผ switch์ (0) | 2023.08.16 |