// for문을 사용해 홀수를 출력해주세요. for(int i = 1; i <= 100; i++) { if(i % 2 == 1) { Console.WriteLine(i); } }
// while 문을 사용해 홀수를 출력해주세요. int count = 1; while(count <= 100) { if(count % 2 == 1) { Console.WriteLine(count); } count++; }
//do-while 문을 사용해 홀수를 출력해주세요. int count = 1; do { if (count % 2 == 1) { Console.WriteLine(count); } count++; } while (count <= 100);
Console.WriteLine("") 을 잊지말자!주어진 수 : 10, 20, 30, 40, 50
int[] num = { 10, 20, 30, 40, 50 }; int sum = 0; for (int i = 0; i < num.Length; i++) { sum += num[i]; } float average = sum / num.Length; Console.WriteLine("Sum: " + sum); Console.WriteLine("Average: "+ average);
int num[5] 같이 선언할 수 없으니 주의하자!for, while, do-while 문 중 하나를 활용하여 해결해주세요.Console.Write("Enter a number: "); int num = int.Parse(Console.ReadLine()); int result = 1; for(int i = num; i >= 1; i--) { result *= i; } Console.Write("Factorial of " + num + " is " + result);
Random random = new Random(); int answer = random.Next(0, 101); while (true) { Console.Write("Enter your guess (1-100): "); int num = int.Parse(Console.ReadLine()); if(answer == num) { Console.WriteLine("Congratulations! You guessed the number."); break; } else if (answer > num) { Console.WriteLine("Too low! Try again."); } else if (answer < num) { Console.WriteLine("Too high! Try again."); } }
random.Next(0,101)은 0~100의 랜덤한 수를 가져오는 것이다.)// 구구단 세로로 출력해 주세요 for(int i = 2; i <= 9; i++) { for(int j = 1; j <= 9; j++) { Console.Write(i + " x " + j + " = " + i * j + " "); } Console.WriteLine(); }
// 구구단 가로로 출력해 주세요 for(int i=1; i <= 9; i++) { for(int j=2; j <= 9; j++) { Console.Write(j + " x " + i + " = " + i * j + " "); } Console.WriteLine(); }
Console.WriteLine();을 사용해 최대값과 최소값을 각각 출력할 수 있습니다.int[] numbers = { 10, 20, 30, 40, 50 }; int min = 2147483647; int max = -2147483648; for(int i = 0; i < numbers.Length; i++) { if (numbers[i] > max) { max = numbers[i]; } if (numbers[i] < min) { min = numbers[i]; } } Console.WriteLine("최대값: " + max); Console.WriteLine("최소값: " + min);
게임설명 : 행맨 게임은 사용자가 단어를 추측하는 게임입니다. 사용자는 알파벳을 하나씩 입력하고, 맞출 때마다 단어의 해당 위치에 문자가 표시됩니다. 틀릴 경우, 기회가 줄어듭니다.
배열 사용 : char[] 배열을 사용하여 단어를 저장하고, 추측된 문자를 저장합니다.
반복문 사용 : 게임은 사용자가 단어를 맞추거나 기회가 끝날 때까지 반복됩니다.
조건문 사용 : 각 입력된 문자가 단어에 포함되는지 확인합니다.
secretWord: 맞춰야 할 단어입니다. 예제에서는 "hangman"으로 설정되어 있습니다.guessWord: 사용자가 맞춘 문자를 저장하는 문자 배열로, 초기에는 언더스코어(_)로 채워져 있습니다.attempts: 사용자가 틀릴 수 있는 기회의 수로, 초기에는 6으로 설정되어 있습니다.wordGuessed: 사용자가 단어를 모두 맞췄는지를 나타내는 불리언 변수입니다.string secretword = "hangman"; char[] secretArr = new char[secretword.Length]; for (int i = 0; i < secretword.Length; i++) { secretArr[i] = secretword[i]; } char[] guessword = new char[secretword.Length]; for (int i = 0; i < guessword.Length; i++) { guessword[i] = '_'; } int attempts = 6; bool wordGuessed = false; bool Correct(int num) { for (int i = 0; i < secretArr.Length; i++) { if (secretArr[i] == num) { guessword[i] = secretArr[i]; secretArr[i] = '#'; return true; } } return false; } string CharToString(char[] c) { string temp = ""; for (int i = 0; i < guessword.Length; i++) { temp += guessword[i]; } return temp; } while (attempts != 0) { Console.Write("알파벳을 입력해주세요: "); char guessAlpha = char.Parse(Console.ReadLine()); if (Correct(guessAlpha)) { if (CharToString(guessword) == secretword) { wordGuessed = true; break; } else { Console.WriteLine("해당 알파벳은 문장에 포함되어 있습니다!"); Console.WriteLine(CharToString(guessword)); } } else { attempts--; Console.WriteLine("틀렸습니다. 남은 목숨: " + attempts); } } if (wordGuessed) { Console.WriteLine("정답!"); Console.WriteLine(CharToString(guessword)); } else { Console.WriteLine("실패!"); Console.WriteLine(CharToString(guessword)); }
Correct() 함수 부분 인 것 같습니다. 사용자가 입력한 알파벳이 문장에 포함되어 있는지 확인하는 함수인데, 알파벳을 맞춘 부분을 임의의 문자(저는 '#'으로 지정했습니다)로 바꿔주지 않으면, 문장에 중복된 문자가 있을 경우에 잘못될 수 있습니다. 예를 들어 "hangman"이라는 문자열에는 a가 2개 n이 2개 존재하는데, 앞에 존재하는 알파벳이 임의의 문자로 바뀌지 않으면 Correct() 함수 호출 시 계속해서 앞부분의 중복된 알파벳만 탐색될 것 입니다.숫자 야구 게임은 3자리의 숫자를 맞추는 게임입니다. 컴퓨터가 고른 3자리 숫자는 모두 다른 숫자로 이루어져 있습니다. 사용자는 3자리 숫자를 입력하고, 컴퓨터는 입력한 숫자와 자리수를 비교하여 스트라이크와 볼의 개수를 알려줍니다.
예를 들어, 컴퓨터가 427을 선택하고 사용자가 123을 입력했을 때, 2는 맞지만 자리수가 다르므로 1볼, 1은 맞지 않으므로 0스트라이크입니다. 이 과정은 사용자가 정확한 숫자를 맞출 때까지 반복됩니다.
Random random = new Random(); int[] targetNumber = new int[3]; bool Check(int[] arr, int x) { for (int i = 0; i < 3; i++) { if (arr[i] == x) { return true; } } return false; } for (int i = 0; i < 3; i++) { while (true) { int target = random.Next(1, 10); if (Check(targetNumber, target)) { continue; } else { targetNumber[i] = target; break; } } } int[] userGuess = new int[3]; int attempts = 0; bool guessedCorrectly = false; while (true) { attempts++; int strike = 0; int ball = 0; Console.Write("Enter your guess (3 digits): "); string num = Console.ReadLine(); for (int i = 0; i < 3; i++) { userGuess[i] = num[i] - '0'; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (targetNumber[i] == userGuess[j]) { if (i == j) { strike++; } else { ball++; } } } } if (strike == 3) { guessedCorrectly = true; break; } else { Console.WriteLine(strike + " Strike(s), " + ball + " Ball(s)"); } } if (guessedCorrectly) { Console.WriteLine("Congratulations! You\'ve guessed the number in " + attempts + " attempts."); }