C#/수업내용

2020.04.16. 수업내용 - 문자열 배열 출력, 배열값 거꾸로 출력하기(foreach)

dev_sr 2020. 4. 16. 21:53

 

1. 입력받은 문자열 배열 출력하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_010
{
    class App
    {
        public App()
        {
           
            string[] strFruit = new string[3];
            for (int i = 0; i < strFruit.Length; i++)
            {
                Console.Write("좋아하는 과일을 입력해주세요.  ");
                strFruit[i] = Console.ReadLine();
            }
            foreach (string fruit in strFruit)
            {
                Console.WriteLine(fruit);
            }
            Console.WriteLine("를 좋아하시는군요");
          
        }
    }
}

 

1) 결과값

 

 

 

 

 

 

2. 입력받은 배열값 거꾸로 출력하기

 

 

1) App class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class App
    {
        public App()
        {
            
 
            Fruit[] arrFruits = new Fruit[3];
 
            for (int i = 0; i < arrFruits.Length; i++)
            {
                Console.Write("좋아하는 과일을 입력해주세요  ");
                arrFruits[i] = new Fruit(Console.ReadLine());
            }
 
            Console.WriteLine();
 
            int index = arrFruits.Length - 1;
 
            foreach (Fruit fruit in arrFruits)
            {
                Console.WriteLine("{0}, {1}, {2}", index, arrFruits[index], arrFruits[index].name);
                index--;
            }
            Console.WriteLine("\n를 좋아하시는군요");
 
        }
    }
}
 
 

 

 

2) Fruit class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class Fruit
    {
        public string name;
        public Fruit(string name)
        {
            this.name = name;
        }
    }
}
 
 

 

3) 결과값