카테고리 없음
2018.03.13 수업내용
일등하이
2018. 5. 15. 21:20
반응형
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //2018.03.13 class Program { static void Main(string[] args) { //국어, 수학, 과학 점수를 각각 입력 받아 학생의 총점과 평균을 구하는 프로그램을 작성하세요. //배열을 사용해주세요. string[] subjects = new string[3] { "국어", "수학", "과학" }; string[] names = new string[3] { "홍길동", "임꺽정", "김기사" }; int[] scores = new int[3] { 45, 20, 100 }; Console.Write(names[0] + "의 " + subjects[0] + " 점수를 입력해주세요."); int score; //성공했을경우에 문자열이 숫자로 변환되서 값이 들어감 bool result = int.TryParse(Console.ReadLine(), out score); //입력: 점수 (정수) //result -> 성공: true, 실패 : false Console.WriteLine("result: " + result + " , score: " + score); if (result) { scores[0] = score; } Console.Write(names[1] + "의 " + subjects[0] + " 점수를 입력해주세요."); result = int.TryParse(Console.ReadLine(), out score); //입력: 점수 (정수) if (result) { scores[1] = score; } Console.Write(names[2] + "의 " + subjects[0] + " 점수를 입력해주세요."); result = int.TryParse(Console.ReadLine(), out score); //입력: 점수 (정수) if (result) { scores[2] = score; } Console.ReadKey(); } } } | cs |
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //2018.03.13 class Program { static void Main(string[] args) { string[] arrFruitNames = { "바나나", "복숭아", "사과", "레몬" }; int i = 0; foreach (string fruitName in arrFruitNames) { Console.Write(i + " : " + fruitName); Console.WriteLine(); i++; } Console.ReadKey(); } } } | cs |
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 50 51 52 53 54 55 56 57 58 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //2018.03.13 class Program { static void Main(string[] args) { string[] categoryNames = new string[2] { "과자", "음료수" }; ArrayList inventory = new ArrayList(); string banana = "바나나"; inventory.Add(banana); inventory.Add("귤"); inventory.Add("복숭아"); inventory.Add(100); inventory.Add(true); inventory.Add(1.0f); inventory.Add(1 > 10); inventory.Add(categoryNames); foreach (var item in inventory) { Console.WriteLine(item); } Console.WriteLine("=================="); inventory.Remove("귤"); foreach (var item in inventory) { Console.WriteLine(item); } Console.WriteLine("=================="); //inventory.Add("귤"); //끝에 들어감 inventory.Insert(0, "귤"); foreach (var item in inventory) { Console.WriteLine(item); } int idxBanana = inventory.IndexOf("바나나"); Console.WriteLine("바나나의 인덱스 : " + idxBanana); Console.ReadKey(); } } } | cs |
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 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //2018.03.13 class Program { static void Main(string[] args) { //포스기기만들기 //상품을 입력받아 ArrayList에 저장하고 각격을 출력 하는 프로그램을 작성하세요. //예 //담당자 이름을 입력해주세요. [입력] //상품 목록 : 바나나(200), 복숭아(600), 수박(500) //상품을 입력해주세요. [입력] 바나나 //상품을 입력해주세요. [입력] 복숭아 //상품을 입력해주세요. [입력] 수박 //============== //영수증 출력 //바나나 : 200원 //복숭아 : 600원 //수박 : 500원 //============== //합계 : 1300원 //담당자: 홍길동 //============== //상품을 입력해주세요. [입력] 메뉴 //[메뉴] 1.입력, 2.환불, 3.계산 //메뉴를 선택해주세요. [입력] 2 //제거할 상품을 입력해주세요. [입력] 수박 //제거할 상품을 입력해주세요. [입력] 복숭아 //제거할 상품을 입력해주세요. [입력] 계산 //영수증 출력 Console.ReadKey(); } } } | cs |
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp2 { class Program { static void Main(string[] args) { string[,] array = new string[2, 2]; array[0, 0] = "바나나"; array[1, 0] = "복숭아"; array[0, 1] = "귤"; array[1, 1] = "수박"; for (int y = 0; y < 2; y++) { for (int x = 0; x < 2; x++) { Console.Write(array[x, y] + " "); } Console.WriteLine(); } Console.ReadKey(); } } } | cs |
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; namespace ConsoleApp2 { class Program { static void Main(string[] args) { int[,] map = new int[2, 2]; map[0, 0] = 0; map[1, 0] = 0; map[0, 1] = 0; map[1, 1] = 0; for (int y = 0; y < 2; y++) { for (int x = 0; x < 2; x++) { //x = 0, y = 0 //x = 1, y = 0 //x = 0, y = 1 //x = 1, y = 1 map[x, y] = 0; } } Console.ReadKey(); } } } | cs |
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 50 51 52 53 54 55 56 57 58 59 60 61 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp3 { class Program { static void Main(string[] args) { string[,] array = new string[4,4]; //0,0 1,0 2,0 3,0 //0,1 1,1 2,1 3,1 //0,2 1,2 2,2 3,2 //0,3 1,3 2,3 3,3 foreach (var e in array) { Console.WriteLine("===>" + e); } string path = @"D:\workspace\0312\console\ConsoleApp3\ConsoleApp3\map.txt"; string[] lines = System.IO.File.ReadAllLines(path); int[,] map01 = new int[10,10]; int i = 0; foreach (string line in lines) { var arrX = line.Split(','); for (int j = 0; j < 10; j++) { map01[i, j] = int.Parse(arrX[j]); } i++; } for(int k = 0; k<10; k++) { for (int l = 0; l < 10; l++) { if (l < 9) Console.Write(map01[k, l] + " , "); else Console.Write(map01[k, l]); } Console.WriteLine(); } Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } } | cs |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { //2018.03.13 class Program { static void Main(string[] args) { //포스기기만들기 "끝" : 프로그램 종료 //상품을 입력받아 ArrayList에 저장하고 각격을 출력 하는 프로그램을 작성하세요. //담당자 이름을 입력해주세요. [입력] ArrayList cartList = new ArrayList(); Console.Write("담당자 이름을 입력해주세요."); string managerName = Console.ReadLine(); //상품 목록 : 바나나(200), 복숭아(600), 수박(500) string[] arrProductName = new string[3] { "바나나", "파프리카", "수박" }; int[] arrProductPrice = new int[3] { 200, 600, 500 }; int[] arrProductCount = new int[3]; //상품을 입력해주세요. [입력] 바나나 string productName = ""; bool isAdd = true; while (true) { if( isAdd ) Console.Write("[추가] 상품을 입력해주세요."); else Console.Write("[환불] 상품을 입력해주세요."); productName = Console.ReadLine(); if (productName == "끝") { } else { switch (productName) { case "메뉴": Console.Write("[메뉴] 1.입력, 2.환불, 3.계산, 4.목록"); int selectNo; if (int.TryParse(Console.ReadLine(), out selectNo)) { switch (selectNo) { case 1: //입력 isAdd = true; break; case 2: //환불 isAdd = false; break; case 3: //계산 if (cartList.Count > 0) { int sum = 0; Console.WriteLine("================== [영수증] =================="); foreach (string product in cartList) { var arrProduct = product.Split(':'); int idx = Array.IndexOf(arrProductName, arrProduct[0]); sum += int.Parse(arrProduct[1]) * arrProductCount[idx]; Console.WriteLine(arrProduct[0] + "x " + arrProductCount[idx] + " : " + String.Format("{0:#,##0}", (int.Parse(arrProduct[1]) * arrProductCount[idx])) + "원"); } Console.WriteLine("============================================"); Console.WriteLine("합계: " + String.Format("{0:#,##0}", sum)); Console.WriteLine("============================================"); } else { Console.WriteLine("가방이 비어있습니다."); } break; case 4: //목록 Console.WriteLine("========= [상품 목록] ========="); for (int i = 0; i < arrProductName.Length; i++) { Console.Write(arrProductName[i]); Console.Write(" "); Console.Write((String.Format("{0:#,##0}", arrProductPrice[i]) + "원")); Console.WriteLine(); } Console.WriteLine("================================"); break; } } break; default: if (arrProductName.Contains(productName)) { int idx = Array.IndexOf(arrProductName, productName); int price = arrProductPrice[idx]; string temp = productName + ":" + price; //입력 상태 if (isAdd) { bool hasProduct = cartList.Contains(temp); if (hasProduct) { arrProductCount[idx]++; } else { arrProductCount[idx]++; cartList.Add(temp); } Console.WriteLine(temp + " 추가 됨." + " , " + arrProductCount[idx]); } else { //환불상태 bool hasProduct = cartList.Contains(temp); if (hasProduct) { if (arrProductCount[idx] > 1) { --arrProductCount[idx]; } else { cartList.Remove(temp); } Console.WriteLine(temp + " 제거 됨."); } else { Console.WriteLine("가방이 비어 있습니다."); } } } else { Console.WriteLine("그런 상품은 없는데요"); } break; } } } //상품을 입력해주세요. [입력] 복숭아 //상품을 입력해주세요. [입력] 수박 //상품을 입력해주세요. [입력] 메뉴 //[메뉴] 1.입력, 2.환불, 3.계산, //메뉴를 선택해주세요. [입력] 2 //제거할 상품을 입력해주세요. [입력] 수박 //제거할 상품을 입력해주세요. [입력] 복숭아 //영수증 출력 //================== //바나나 : 200원 //복숭아 : 600원 //수 박 : 500원 //================== //합 계 : 1,300원 //담당자 : 홍길동 //================== } } } |
반응형