# 리뷰: 칼로리 계산 칼로리나 그램을 수정하고 싶을때 메소드 리턴으로 내려가지 않고 제일 상단에서 수정하고 싶어서 인수를 설정(?)했는데요 이 인수를 리턴 계산식에다가 적으면 안되나요? #### CODE <a class='btn btn-default' href='/codes/46010'>Link</a> ``` public class Pork { int person=3; int gram = 180; double kcal = 5.179; public static void main(String[] args) { /* 1. 변수를 생성하시오. */ /* 2. 메소드를 통한 칼로리를 계산하시오. */ double calculatedkcal = calculateKcal(person); /* 3. 결과를 출력하시오 */ System.out.printf("삼겹살 %d인분: %.2fkcal", person, calculatedkcal); } /* 4. 칼로리 계산을 위한 메소드를 작성하시오. */ public static double calculateKcal(int p){ return p*gram*kcal; } } ``` #### INPUT ``` ``` #### OUPUT ``` /root/var/tmp/2021_05_16_07_44_53_9cf92422/Pork.java:10: error: non-static variable person cannot be referenced from a static context double calculatedkcal = calculateKcal(person); ^ /root/var/tmp/2021_05_16_07_44_53_9cf92422/Pork.java:12: error: non-static variable person cannot be referenced from a static context System.out.printf("삼겹살 %d인분: %.2fkcal", person, calculatedkcal); ^ /root/var/tmp/2021_05_16_07_44_53_9cf92422/Pork.java:17: error: non-static variable gram cannot be referenced from a static context return (double)p*gram*kcal; ^ /root/var/tmp/2021_05_16_07_44_53_9cf92422/Pork.java:17: error: non-static variable kcal cannot be referenced from a static context return (double)p*gram*kcal; ^ 4 errors ```
## 아직 객체와 static을 배우지 않은 관계로, 메소드 내부의 변수를 사용하는 것이 좋습니다. ### static 메소드에서는 static 필드만 직접 사용할 수 있습니다. ### static을 배웠다면 다음과 같은 방식으로 코드를 작성할 수 있습니다. ``` public class Pork { static int person=3; static int gram = 180; static double kcal = 5.179; public static void main(String[] args) { double calculatedkcal = calculateKcal(person); System.out.printf("삼겹살 %d인분: %.2fkcal", person, calculatedkcal); } public static double calculateKcal(int p){ return p*gram*kcal; } } ```