본문 바로가기

프로그래밍 언어/Java

Java programming 자바 프로그래밍 실습 : 원기둥 부피 구하기

Java programming 자바 프로그래밍 실습 : 원기둥 부피 구하기


2021-1 성균관대학교 소프트웨어학과 타메르 교수님의 자바 프로그래밍 실습 수업을 들으면서 수행한 예제입니다

 

문제 

원기둥의 부피를 구하는 자바 어플리케이션을 개발하라.

프로그램은 원기둥의 반지름과 높이를 입력받고, 그 결과를 출력한다. 

// input
Radius (cm): 8
Height (cm): 15

// output
Volume of the cylinder: 3014.4
import java.util.Scanner;


public class Task1 {

	public static void main(String[] args) {
		
		// declare variables to use
		// r is to store radius, h is for height, result is for the result of calculation
		int r, h;
		double result;
		
		// declare Scanner to get console input
		Scanner input = new Scanner(System.in);
	
		System.out.print("Radius (cm): ");
		r = input.nextInt();
		System.out.print("Height (cm): ");
		h = input.nextInt();
		
		// this is for closing input stream (I refer to the QA board on Canvas.)
		input.close();
		
		// calculate the volume of the cylinder with inputs
		result = 3.24 * r * r * h;
		
		// print result
		System.out.println("Volume of the cylinder: " + result);
	}

}