본문 바로가기
IT

[C++ & Java] 99병의 맥주

by %? 2022. 1. 21.

99 Bottles of Beer - C++

#include "stdio.h"

void main()
{
	printf("99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall.\n");

	for (int i = 98; i > 1; i--)
	{
		if (i == 2)
		{
			printf("2 bottles of beer on the wall, 2 bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall.\n");
		}
		else
		{
			printf("%d bottles of beer on the wall, %d bottles of beer. Take one down and pass it around, %d bottles of beer on the wall.\n", i, i, i-1);
		}
	}

	printf("1 bottle of beer on the wall, 1 bottle of beer. Take it down and pass it around, no more bottles of beer on the wall.\nNo more bottles of beer on the wall, no more bottles of beer.Go to the store and buy some more, 99 bottles of beer on the wall.\n");
}

99 Bottles of Beer - Java

public class MyClass {
	public static void main(String args[]) {

		System.out.println("99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall.");

		for(int i=98; i>1; i--)
		{
			if(i==2)
			{
				System.out.println("2 bottles of beer on the wall, 2 bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall.");
			}
			else
			{
				System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer. Take one down and pass it around, " + (i-1) + " bottles of beer on the wall.");
			}
		}

		System.out.println("1 bottle of beer on the wall, 1 bottle of beer. Take it down and pass it around, no more bottles of beer on the wall.");
		System.out.println("No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.");
	}
}

코딩 연습에 괜찮다길래 메모장에 대충 적어서 확인해봤습니다.
확실히 괜찮은게 문장 뒤에는 변수-1의 값이 들어가야하고, 마지막에는 복수형에서 단수형으로 출력하는 부분도 있어요.
그리고 개인적으로는 뭐 파일을 추가 시켜서 텍스트 입력을 받게 한다거나 숫자의 패턴을 바꾸는 등 다양한 변형이 가능할거 같네요 :)