2020. 9. 1. 18:19ㆍBACK-END/JAVA
지역변수
자기 지역 안에서만 사용 할 수 있는 변수를 지역변수라고 한다.
지역변수에는 두가지 성질이 있는데
첫 번째 성질
{
int a = 10;
}
System.out.println(a);
라고 선언한다면 eclipse 에서 "a cannot be resolved to a variable"(a라는 변수가 무엇인지 모르겠습니다) 라는 에러가 생긴다.
int a 는 지역변수이므로 지역이 떠남으로 인해 없음으로 인식하게 된다.
지역변수는 자신이 속해 있는 지역에서만 사용 가능하다.
두번째 성질
int a = 20;
{
int a = 10;
System.out.println(a);
}
지역변수 밖의 변수가 있으므로 지역변수를 사용할 수 없다.
지역 밖의 변수는 내부에서 다시 선언할 수 없으나 지역안에스 밖의 있는 변수를 사용 할 수 있다.
{
int a = 10;
System.out.pirntln(a);
}
{
System.out.println(a);
}
지역 변수는 그 지역에서만 사용할 수 있으므로 사용하려면 해당 지역 안에 지역변수를 다시 선언 해야한다.
예제
int a = 20;
{ System.out.print(a + " , ");
a = 50;
}
{
System.out.println(a);
}
위 코드의 출력 값은 무엇 일까.
출력값은 20 , 50 이다.
지역 변수로 인해 첫 번째 지역의 변수 a 는 지역 밖의 변수의 값인 20이 되고
두 번째 지역의 변수인 a 는 첫 번째 지역에서 a = 50; 을 선언하였기에 50이 되어 출력값은 20 , 50 이다.
활용하여 다른 예제를 풀어본다면
활용예제
import java.util.Scanner;
public class Homework_01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=== 계산기 프로그램 ===");
while(true) {
System.out.print("연산자 입력 ( +, -, *, / ) :");
String msg = sc.nextLine();
if(msg.contentEquals("q") || msg.contentEquals("Q")) {
System.out.println("계산기 프로그램을 종료합니다.");
break;
}else if(msg.contentEquals("+") && msg.contentEquals("-") &&
msg.contentEquals("*") && msg.contentEquals("/")) {
System.out.println("*** 연산자를 다시 확인하세요 ***");
continue;
}
while(true) {
try {
System.out.print("첫 번째 숫자 : ");
double one = Integer.parseInt(sc.nextLine());
System.out.print("두 번째 숫자 : ");
double two = Integer.parseInt(sc.nextLine());
break;
}catch (Exception e) {
System.out.println("숫자를 다시 확인 해주세요.");
continue;
}
}
System.out.println("===== 결과 =====");
switch(msg) {
case "+" :
System.out.println(" " + one +" + "+ two + " = "+ (one + two));
break;
case "-" :
System.out.println(" " + one +" - "+ two + " = "+ (one - two));
break;
case "*" :
System.out.println(" " + one +" * "+ two + " = "+ (one * two));
break;
case "/" :
System.out.println(" " + one +" / "+ two + " = "+ (one / (double)two));
break;
}
System.out.println("===========================================");
}
}
}
switch 문법 부분의 출력 문구들에 에러가 난다.
import java.util.Scanner;
public class Homework_01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=== 계산기 프로그램 ===");
double one = 0;
double two = 0;
while(true) {
System.out.print("연산자 입력 ( +, -, *, / ) :");
String msg = sc.nextLine();
if(msg.contentEquals("q") || msg.contentEquals("Q")) {
System.out.println("계산기 프로그램을 종료합니다.");
break;
}else if(msg.contentEquals("+") && msg.contentEquals("-") &&
msg.contentEquals("*") && msg.contentEquals("/")) {
System.out.println("*** 연산자를 다시 확인하세요 ***");
continue;
}
while(true) {
try {
System.out.print("첫 번째 숫자 : ");
one = Integer.parseInt(sc.nextLine());
System.out.print("두 번째 숫자 : ");
two = Integer.parseInt(sc.nextLine());
break;
}catch (Exception e) {
System.out.println("숫자를 다시 확인 해주세요.");
continue;
}
}
System.out.println("===== 결과 =====");
switch(msg) {
case "+" :
System.out.println(" " + one +" + "+ two + " = "+ (one + two));
break;
case "-" :
System.out.println(" " + one +" - "+ two + " = "+ (one - two));
break;
case "*" :
System.out.println(" " + one +" * "+ two + " = "+ (one * two));
break;
case "/" :
System.out.println(" " + one +" / "+ two + " = "+ (one / (double)two));
break;
}
System.out.println("===========================================");
}
}
}
while(true) //무한루프 부분의 one , two 두 개의 변수를 while(true) 지역의 밖인 맨 위로 올려 double one = 0; , double two = 0; 으로 선언하고 while(true) 지역의 double one , double two 부분의 double을 지워 선언하면
밑에 swtich 문법 지역의 에러를 처리 할 수 있다.