public static void main(String[] args) { int i = 1, j =2; System.out.println("구구단"); while ( i <= 9) { while (j <= 9) { System.out.print(j + " * " + i + " = " + i*j + "\t"); j++; } System.out.println(); j = 2; i ++; } }
- break 는 해당하는 값이 있는면 가장 가까운 반복문을 빠져 나간다
- while, for, do-while, switch에서 사용된다.
public static void main(String[] args) { int num = 0, sum = 0; while (true) { num ++; sum += num; //sum += ++num; System.out.println("sum-> "+sum); if(num==5) break; } System.out.println("합계: "+ sum); System.out.println("끝"); } sum-> 1 sum-> 3 sum-> 6 sum-> 10 sum-> 15 합계: 15 끝
- 조건과 일치하게 되면 다시 근처 반복문으로 돌아가서 실행하게 된다.
for (int i = 0; i < 10 ; i ++) { System.out.println("대박 i = " + i); if (i > 5) continue; System.out.println("쪽박 i = " + i); } } 대박 i = 0 쪽박 i = 0 대박 i = 1 쪽박 i = 1 대박 i = 2 쪽박 i = 2 대박 i = 3 쪽박 i = 3 대박 i = 4 쪽박 i = 4 대박 i = 5 쪽박 i = 5 대박 i = 6 대박 i = 7 대박 i = 8 대박 i = 9
public static void main(String[] args) { String strVar1 = "신민철"; //처음에 신민철이라는걸 메모리에 저장한다 String strVar2 = "신민철"; // 같은게 있는지 저장하기 전에 메모리를 뒤져보고 있으면 같은 주소를 가르킨다. if(strVar1 == strVar2) { //주소가 같은지 비교, System.out.println("strVar1과 strVar2는 참조가 같음"); } else { System.out.println("strVar1과 strVar2는 참조가 다름"); } if(strVar1.equals(strVar2)) { //문자열이 같은지 비교 System.out.println("strVar1과 strVar2는 문자열이 같음"); } System.out.println(); // ---------------------------------------------------------------- String strVar3 = new String("신민철"); //각각에 객체에 신민철을 넣은거라서 주소가 다름 String strVar4 = new String("신민철"); if(strVar3 == strVar4) { //주소가 같은지 비교, System.out.println("strVar1과 strVar2는 참조가 같음"); } else { System.out.println("strVar1과 strVar2는 참조가 다름"); } if(strVar3.equals(strVar4)) { //문자열이 같은지 비교 System.out.println("strVar1과 strVar2는 문자열이 같음"); } }
strVar1과 strVar2는 참조가 같음
strVar1과 strVar2는 문자열이 같음
strVar1과 strVar2는 참조가 다름
strVar1과 strVar2는 문자열이 같음
- 배열 선언은 여러 방법으로 사용할 수 있다.
- int [ ] a = new int [ 3 ] ;
a[ 0 ] = 1;
a[ 1 ] = 2;
a[ 2 ] = 3;- int [ ] a = new int [ ] { 1, 2, 3 } ;
- int [ ] a = { 1 , 2, 3 };
- 위 세가지 모두 사용 가능하지만 3번이 가장 사용하기 쉽다.
- 일반 for문 보다는 배열을 사용할때에는 향상형 for문을 주로 사용한다.
public static void main(String[] args) { String[] str = {"코로나", "이강인", "손흥민", "김민재", "우영우"}; for (int i = 0; i < str.length ; i++) { System.out.println(str[i]); } System.out.println("------------향상형 for ---------------"); for (String kk : str) { System.out.println("KK = "+kk); } }
public static void main(String[] args) { int sum = 0, avg = 0, max = 0, min = 100; //초기값을 max를 0과 min을 100으로 해야지 처음 배열을 가져오는 값을 int[] a = {76,45,34,89,100,50,90,92}; for(int i = 0 ; i < a.length ; i++) { sum += a[i]; if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } avg = sum / a.length; System.out.println("합계 = " + sum + " 평균 = " + avg); System.out.println("최대값은 = " + max + " 최소값은 = " + min); }
합계 = 576 평균 = 72
최대값은 = 100 최소값은 = 34
- call by referense : 기본타입을 제외한 것을 불러올 경우, 값을 가져오는게 아니라 주소를 가져온다.
- call by value : 기본 타입을 불러올 경우, 값을 가져온다
public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7}; int[] b = new int [10]; System.arraycopy(a, 0, b, 0, a.length); kkk(35); // call by value 이므로 값을 kkk에 보내는 것 왜냐면 35는 기본타입이니(int) kkk3(a); // call by reference 이므로 주소을 kkk3에 보내는 것(배열이라서) kkk3(b); // call by reference 이므로 주소을 kkk3에 보내는 것(배열이라서) } public static void kkk(int kk) { //값을 받아옴 System.out.println(kk); } public static void kkk3(int[] rr) { // 주소를 받아옴 for (int i = 0; i < rr.length ; i++) { System.out.print(rr[i] + "\t"); } System.out.println(); }
35
1 2 3 4 5 6 7
1 2 3 4 5 6 7 0 0 0