Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- FileWriter
- iframe 태그
- FFT
- BorderLayout
- inline
- 푸리에 변환
- java
- html 프로젝트
- g검정
- 예제
- 파이썬
- 메서드
- CSS
- 상속
- css 기초
- 반응형 웹 프로젝트
- rnn
- 퍼셉트론
- ObjectOutputStream
- 반응형웹
- Database
- html 기초
- HTML
- oracle
- GridLayout
- 미디어쿼리
- Position
- Codility
- FlowLayout
- 사전학습
Archives
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 18일차 본문

지역 변수와 전역 변수
- 지역 변수(local variable)
: 함수 내에 위치한 변수. 함수가 시작할 때 메모리에 생기고, 함수가 종료되면 메모리에서 없어진다. ( = 함수 안에서만 변수 사용 가능)
- 전역 변수(global variable)
: 함수 외부에 위치한 변수. 프로그램이 시작될 때 메모리에 생기고, 프로그램이 종료되면 메모리에서 없어진다. ( = 모든 함수에서 사용할 수 있음)
예제
배열을 매개변수로 받는 메서드 작성.
import java.util.Scanner;
public class Prac8 {
static int input(int[] score) {
Scanner sc = new Scanner(System.in);
int total=0;
for (int i = 0; i < score.length; i++) {
System.out.print((i+1) + "번 학생의 점수를 입력 : ");
score[i] = sc.nextInt(); // Add each score in score array
total += score[i]; // sum of score
}
return total;
}
static double calc_avg(int total, int[] score) {
double avg = (double)total/score.length;
return avg;
}
static void output(int total, double avg) {
System.out.println("총점 : " + total + ", 평균 : " + avg);
}
public static void main(String[] args) {
// 선언
int[] score = new int[5]; // create score array
int total = 0; // total score
double avg = 0;
// 입력, 연산
total = input(score);
// 연산
avg = calc_avg(total, score);
// 출력
System.out.println();
output(total, avg);
}
}'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 22일차 (0) | 2022.10.17 |
|---|---|
| [Java/Eclipse] Java 19 - 21일차 (0) | 2022.10.17 |
| [Java/Eclipse] Java 17일차 (0) | 2022.10.07 |
| [Java/Eclipse] Java 15, 16일차 (0) | 2022.10.06 |
| [Java/Eclipse] Java 14일차 (1) | 2022.10.04 |