C#/수업내용

2020.04.06. 수업내용 - for문, if문 1 (목록, 쉼표 빼기)

dev_sr 2020. 4. 6. 23:46

 

3. 입력받은 문자열로 목록을 만들고 마지막 단어 쉼표 빼기

4. 입력 받은 횟수만큼 반복문 실행하기 (0이면 종료한다)

 

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
38
39
40
41
42
43
44
45
46
47
48
49
            //3. 좋아하는 과일 무한으로 물어보기(목록, 종료)
 
            string fruitList = "";
 
            for (; ;)
            {
                Console.Write("좋아하는 과일을 입력하세요 ");
                var input = Console.ReadLine();
 
                if (input == "종료")
                {
                    Console.WriteLine("종료합니다");
                    break;
                }
                else if (input=="목록")
                {
                    var strFruitList = fruitList.Substring(0, fruitList.Length - 1); //마지막 단어 쉼표빼기
                    Console.WriteLine(strFruitList);
                }
                else
                {
                    Console.WriteLine("{0}를/을 좋아하시는군요\n", input);
                    fruitList += input+",";
                }
            }
 
            Console.WriteLine("--------------------------------------");
 
 
            //4. 줄넘기 할 횟수 적고 그만큼 하고 마치기
 
            for(; ;)
            {
                Console.Write("줄넘기를 할 횟수를 적어주세요. ");
                int lineCount = Int32.Parse(Console.ReadLine());
 
                for(int i=1; i<=lineCount; i++)
                {
                    Console.WriteLine("줄넘기를 {0}회 하였습니다.\n", i);
                }
 
                if(lineCount==0)
                {
                    Console.WriteLine("줄넘기를 마칩니다! \n");
                    break;
                }
 
            }
 
            Console.WriteLine("--------------------------------------");
 

 

 

 

3. 결과 값

 

4. 결과 값