arr[][] 행/열로 구성
arr[2][3]
ㅁㅁㅁ
ㅁㅁㅁ
표현
[접근제한자][예약어]반환형 메소드명(매개변수){
void를 썼다-> return을 쓰지 않는다
return을 썼다-> void를 쓰지 않는다
반환형있다면 {}안에는 return이 있다 return 옆에는 리턴값이 있다.
{}안에 return이 있다면 리턴값 (반환값)의 자료형을 반환형 자리에 적는다.
없다면 {}안에는 return이 없다. void라고 작성한다
메소드를 호출한다 -> .메소드명을 쓴다 result.exercise1(); ->호출된것
매개변수가 있다면 result.exercise1(1,2,3,'h'); 로 호출 가능
사용하려면 public void exercise1(int num1,int num2,int num3,char cha){
와 같이 매개변수가 지정되어있어야 사용가능
int num1 = 1;
int num2 = 2; 와 같이 변수가 저장되는것
메소드 접근제한자 를 사용하여 기능 제한이 가능함
public static void main(){
퍼블릭 - 제한없이 사용가능
스테틱 - 객체생셩없이 사용 가능
보이드 - 반환형이 없는상태로 실행
package com.kh.proram;
import java.util.Scanner;
public class ScoreProgram {
// static int kor = 0; // 전역변수로 설정해 주는데 int 앞에 static 붙여서 객체화 해줘야함
// static int eng = 0;
// static int math = 0;
// static int total = 0;
// static double avg = 0;
public static void main(String[] args) {
int kor = 0;
int eng = 0;
int math = 0;
int total = 0;
double avg = 0;
Scanner sc = new Scanner(System.in);
EXIT:
while (true) {
printMainMenu();
int choice = sc.nextInt();
switch (choice) {
case 1:
int [] scores = insertScore(); // 여기 매개변수 안에 변수를 넣어줘야함
kor = scores[0];
eng = scores[1];
math = scores[2];
System.out.println(kor);
break;
case 2:
printScore(kor,eng,math,total,avg);
System.out.println(kor);
break;
case 3:
System.out.println("Good Bye~");
break EXIT;
default:
System.out.println("잘못입력하셨습니다.");
break;
}
}
}
static void printMainMenu() { // 바로바로 사용 위해 static 사용 public은 이 안에서만 쓸거니까 안씀
System.out.println("===== 메인 메뉴 =====");
System.out.println("1. 성적입력");
System.out.println("2. 성적출력");
System.out.println("3. 종료");
System.out.print("선택 : ");
}
static int[] insertScore() { // 매개변수를 받을 수 있도록 해줌
Scanner sc = new Scanner(System.in);
int [] scores = new int[3];
System.out.println("===== 성적 입력 =====");
System.out.print("국어 : ");
scores[0] = sc.nextInt();
System.out.print("영어 : ");
scores[1] = sc.nextInt();
System.out.print("수학 : ");
scores[2] = sc.nextInt();
return scores;
// 여기서 입력된 값은 main 밑에 변수에 저장됨
}
static void printScore(int kor,int eng, int math, int total,double avg) {
total = kor + eng + math;
avg = (double) total / 3;
System.out.println("===== 성적 출력 =====");
System.out.println("국어 : " + kor);
System.out.println("영어 : " + eng);
System.out.println("수학 : " + math);
System.out.println();
System.out.println("총점 : " + total);
System.out.println("평균 : " + avg);
}
}
전역변수를 사용하여 매개변수 쓰지 않고 쓸것인가
지역변수를 사용하여 매개변수와 return을 사용하여 쓸것인가
선택
new 연산자(생성자)를 사용하여 객체를 생성하면 heap 메모리 공간에 서로 다른 자료형의 데이터가 연속으로 나열 할당된 객체 공간이 만들어짐
이것을 인스턴스라고 함
Employee emp = new Employee();
객체 생성이 됨으로써 메소드 안에있는 기능과 속성을 사용 가능해짐.
emp가 객체이며 Employee에 있는 기능을 사용가능해진다.
public class Run {
public static void main(String[] args) {
Exercise_DimArray result = new Exercise_DimArray();
result.exercise6();
객체 생성이 되면? 속성과 기능을 사용할 수 있다.