728x90

//이름을 붙인 引数

//引数를 생략하고 싶을 때 붙이는 디폴트치

//

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello(); //hello world가 대입된다.

SayHello("こんにちは","皆さん"); // 디폴트치 대신에 입력한 것이 들어간다.

//곤니치와 미나상

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

}

 

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello(); //hello world가 대입된다.

SayHello("こんにちは","皆さん"); // 디폴트치 대신에 입력한 것이 들어간다.

SayHello("Good morning!"); // Good morning world가 출력된다.

// 이 경우는 두 번째 변수가 생략되어 world가나온다

 

 

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

   

   

}

///

///

// 名前付き引を理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello(); //hello world가 대입된다.

SayHello("こんにちは","皆さん"); // 디폴트치 대신에 입력한 것이 들어간다.

SayHello("Good morning!"); // Good morning world가 출력된다.

// 이 경우는 두 번째 변수가 생략되어 world가나온다

SayHello(greeting: "こんにちは", target: "皆さん");

SayHello(target: "ネコ先生",greeting:"おはようございます。");

// 순번을 바꾸는 것이 가능하다.

 

    }

   

    public static void SayHello(string greeting = "Hello", string target = "world")

    {

       

        Console.WriteLine(greeting + " " + target);

    }

   

   

}

///

//////////

728x90

설정

트랙백

댓글

728x90

// 디폴트

/////////////////

// のデフォルト値

using System;

 

public class Lesson06

{

    public static void Main()

    {

    Introduce("勇者");

    }

   

    public static void Introduce(string name)

    {

        Console.WriteLine("私は" + name + "です。");

    }

   

}

////

//私は勇者です。

 

//

//

// のデフォルト値

using System;

 

public class Lesson06

{

    public static void Main()

    {

    Introduce("勇者");

    Introduce();

    }

   

    public static void Introduce(string name = "村人")

    {

        Console.WriteLine("私は" + name + "です。");

    }

   

}

//Introduce에서

// 디폴트치를 지정할 수 있다.

// 私は勇者です。
// 私は村人です

//

//

// のデフォルト値

using System;

 

public class Program

{

    public static void Main()

    {

SayHello("Hello");

    }

 

    public static void SayHello(string greeting, string target = "paiza")

    {

        Console.WriteLine(greeting + " " + target);

    }

}

//////

//

728x90

설정

트랙백

댓글

728x90

//RPGの攻ンを作ろう

using System;

 

public class Program

{

    public static void Main()

    {

        string[] players = { "勇者", "", "魔法使い" };

        var rand = new Random();

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

 

        foreach (var player in players)

        {

            var hit = rand.Next(1, 4) * 10;

            Console.WriteLine(player + "はスライムを攻した");

            // 下記にコドを追加する

            enemyHp = enemyHp-hit;

            Console.WriteLine("敵のHP" + enemyHp + "です");

        }

    }

 

    public static int Attack(int enemyHp, int hit)

    {

        enemyHp -= hit;

        return enemyHp;

    }

}

 

//

///

 

// RPGの攻ンを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    {

 

Console.WriteLine("Hello World");

// 변수의 스코프

var num = 0;

if (num == 0)

{

    var message = "paiza";

    Console.WriteLine(message + " " + num);

   

}

// 블록 밖에서는 사용 불가능

// Console.WriteLine(message + " " + num);//error

 

    }

   

   /* public static ()

    {

       

    } */

   

}

/////

///

// RPGの攻ンを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    {

Console.WriteLine("Hello World");

// 변수의 스코프

var num = 0;

if (num == 0)

{

    var message = "paiza";

    Console.WriteLine(message + " " + num);

   

}

// 블록 밖에서는 사용 불가능

// Console.WriteLine(message + " " + num);//error

//message가 없음

 

for(var i = 1; i<5;i++)

{

    var message ="C#";

    Console.WriteLine(message + " " + i);

}

 

// Console.WriteLine(message);

//에러가 뜬다.

    }

 

}

/////////

// 블록 안에 설치된 변수만 실행이 된다.

// 변수의 스코프를 인식할 필요가 있다.

728x90

'프로그래밍' 카테고리의 다른 글

C# paiza 54 - 이름을 붙인 引数  (0) 2020.05.16
C# paiza 53 - 인수의 디폴트치1  (0) 2020.05.16
C# paiza 51 - RPG의 공격 Scene1  (0) 2020.05.16
C# paiza 50 - 스코프  (0) 2020.05.16
C# paiza 49 - 곱셉 메소드 만들기  (0) 2020.05.16

설정

트랙백

댓글

728x90

// 間違い探し

using System;

 

public class Program

{

    public static void Main()

    {

       

        SayHello();

    }

 

    public static void SayHello()

    {

        string message = "Hello";

        message += " ";

        message += "paiza";

        Console.WriteLine(message);

    }

}

//출력: paiza

/////

////

 

//

// RPGの攻ンを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    {

string[] enemies = {"スライム", "モンスタ", "ドラゴン"};

foreach(var enemy in enemies)

{

    //Console.WriteLine("勇者は" + enemy + "を攻した");

    Attack(enemy);

   

   

}

    }

 

    public static void Attack(string target)

    {

        Console.WriteLine("勇者は" + target + "を攻した");

       

    }

}

 

///

///

 

// RPGの攻ンを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    {

string[] enemies = {"スライム", "モンスタ", "ドラゴン"};

foreach(var enemy in enemies)

{

    var rand = new Random();

     var num = rand.Next(10);

    Attack(enemy,num);

   

   

}

 

    }

 

    public static void Attack(string target, int hit)

    {

        Console.WriteLine("勇者は" + target + "を攻した");

        if(hit < 5)

        {

            Console.WriteLine(target + "" + hit + "のダメジをえた");

        }

        else

        {

            Console.WriteLine("クリティカルヒット!" + target + "に100のダメジをえた");

        }

}

}

////

/////

////

///

 

728x90

설정

트랙백

댓글

728x90

/// scope

//////

// スコプを理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

        int a = 10;

        int b = 20;

        int num = Sum(a,b);

        Console.WriteLine(num);

 

       

    }

          public static int Sum(int x, int y)

        {

            return x + y;

           

   

        }

}

//////

///

 

// スコプを理解しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

        int a = 10;

        int b = 20;

        int num = Sum(a,b);

        Console.WriteLine(num);

       

    }

          public static int Sum(int x, int y)

        {

            int a = 3;

            Console.WriteLine("hello" + a);

            // 여기에 a변수와 위의 a변수는 노상관

            // scopeが別れている

            return x + y;

   

        }

}

///////

///a가 두 군에서 나오는데 메소드안에 있는 거는 다르다. 구분된다.

////

728x90

설정

트랙백

댓글

728x90

////곱셈

/////////

// 掛け算メソッドを呼び出してみよう

using System;

 

public class Program

{

    public static void Main()

    {

        // この下にメソッド呼び出しを記述する

        Multiply(3,4);

    }

 

    public static void Multiply(int x, int y)

    {

        Console.WriteLine(x * y);

    }

}

/////

 

///

// 九九の表を作成してみよう

using System;

 

public class Program

{

    public static void Main()

    {

        for(int i = 1; i < 10; i++)

        {

            for(int j = 1; j < 10; j++)

            {

                // ここでメソッドを呼び出す

    Multiply(i,j);

                if(j != 9){

                    Console.Write(", ");

                }

            }

            Console.WriteLine();

        }

    }

 

    public static void Multiply(int x, int y)

    {

        Console.Write(x * y);

    }

}

////

/////

728x90

설정

트랙백

댓글

728x90

// メソッドを作ろう

using System;

 

public class Lesson06

{

    public static void Main()

    //Main도 메소드의 하나이다.

    {

        Console.WriteLine("Hello World");

        SayHello(); //SayHello를 불러온다.

         SayHello(); //SayHello를 불러온다.

    }

    public static void SayHello() //단어의 구분을 대문자

    {

    Console.WriteLine("Hello World2");

    } //block이라고 부른다.

   

   

}

///

/// 스스로 만들어서 할 수도 있다.

///////

// 引数

 

//

// り値を追加しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

SayHello();

// 덧셈 만들기

Sum();

 

    }

    public static void SayHello()

    {

        Console.WriteLine("Hello paiza");

       

    }

    public static void Sum() //메소드 만들기

    {

        Console.WriteLine(10 + 20);

    }

}

///

// 10 + 20이라는 메소드를 만들었다.

///

///

// り値を追加しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

// 덧셈 만들기

Sum(30);

 

    }

    public static void Sum(int x)

    {

        Console.WriteLine(x + 20);

    }

   

}

/// 30 + 20을 만든다.

//////////

 

// り値を追加しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

// 덧셈 만들기

Sum(30,80);

 

    }

    public static void Sum(int x,int y)

    {

        Console.WriteLine(x + y);

    }

   

}

// 30 + 80을 입력할 수 있다.

///

 

//void는 戻り値가 없다. return

// り値を追加しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

// 덧셈 만들기

// Sum(30,80);

int num1 =Sum(3,2);

Console.WriteLine(num1);

 

    }

    public static int Sum(int x,int y)

    {

       // Console.WriteLine(x + y);

        return x+y; //り値お追加

    }

   

}

/////////

/// 모도리치 추가

/// void 戻り値をほとさないとき 지정한다.

// 모도리치가 없다 void

 

// り値を追加しよう

using System;

 

public class Lesson06

{

    public static void Main()

    {

// 덧셈 만들기

// Sum(30,80);

int num1 =Sum(3,2);

Console.WriteLine(num1);

 

int num2 =Sum(35,22);

Console.WriteLine(num2);

    }

    public static int Sum(int x,int y)

    {

       // Console.WriteLine(x + y);

        return x+y; //り値お追加

    }

   

}

///////

//

 

728x90

설정

트랙백

댓글

728x90

// 2次元配列で像を表示する

using System;

 

public class Lesson05

{

    public static void Main()

    {

        //URL用の配列

        string[] playerImages = {

            "https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Empty.png",

            "https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Dragon.png",

            "https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Crystal.png",

            "https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Hero.png",

            "https://paiza-webapp.s3.amazonaws.com/files/learning/rpg/Heroine.png"

        };

 

        //キャラクタ配置用の配列

        int[][] characters = {

            new int[] {1,1,1,1,1},

            new int[] {2,3,3,3,2},

            new int[] {2,4,4,4,2}

        };

 

        Console.WriteLine("<table>");

        foreach (int[] line in characters)

        {

            Console.WriteLine("<tr>");

            foreach (int imageId in line)

            {

                Console.Write("<td><img src='" + playerImages[imageId] + "'></td>");

            }

            Console.WriteLine("</tr>");

        }

        Console.WriteLine("</table>");

    }

}

 

///

//테이블을 불러오는 코드

///

Console.WriteLine("<table>");
foreach (int[] line in characters)
{
    Console.WriteLine("<tr>");
    foreach (int imageId in line)
    {
        Console.Write("<td>" + imageId + "</td>");
    }
    Console.WriteLine("</tr>");
}
Console.WriteLine("</table>");

///

 

/// 이미지를 불러오는 코드

////////

Console.WriteLine("<table>");
foreach (int[] line in characters)
{
    Console.WriteLine("<tr>");
    foreach (int imageId in line)
    {
        Console.Write("<td><img src='" + playerImages[imageId] + "'></td>");
    }
    Console.WriteLine("</tr>");
}
Console.WriteLine("</table>");

//////////

인수: 예를 들면, WriteLine의 경우에는 괄호 안에 있는 것이 引数이다.

Random.Next() 메소드에서는 생성되는 랜덤의 값이 가 되어 rand 변수에 대입된다.

 

Var rand = random.Next(10);의 예에서

메소드에 의해 긴 코드를 줄여서 사용할 수 있다. 몇 번이고 불러오는 것이 가능하다. 여러 가지 변수를 쿠미아와세해서,

같은 코드를 한 곳에 모으는 것이 가능하다. 반복되는 코드를 또 처리할 필요가 없어서 편리하다.

메소드를 만들어보거나 처리하거나 해보자.

728x90

설정

트랙백

댓글