티스토리 뷰

package condition;
import java.util.Scanner;
//@==annotation

public class WhileTest {
	public void ex1() {		//매개 변수가 없고 반환값도 없는 메서드 ex1을 정의
		int x=1; 		// 5. 초기값이 1인 정수형 지역변수를 선언 (cf.전역변수)
		while (x<=10) {		// 6. 반복 조건이 10보다 작거나 같은 while문 기술
			System.out.println(x);		// 7. while문 안에서 5에서 선언한 변수값을 출력
			x++;		// 8. 5에서 선언한 변수값을 1증가
		}
	}
	
	public WhileTest() {		// 2. 매개변수가 없는 생성자를 정의
		this.ex1();		//생성자 안에서 클래스 자신이 갖고 있는 메서드 ex1()를 호출
	}

	public static void main(String[] args) {
		// 1. 매개변수가 없는 생성자를 사용한 객체 생성
		WhileTest play = new WhileTest();
	}

}
  • 메인함수(클래스) 바깥에 기술할 수 있는 단 네가지
    • package
    • import
    • @(annotation)
    • 주석(/* ~*/)(//)


System.out.printf("서식", 값1, 값2, ...);


%d : 정수형값

%f : 실수형값

%c : 문자형값

%s : 문자열형 값


%10d : 10자리를 확보한 후 정수형 값 출력

%10.2f : 10자리 중 두 자리를 소수점으로 출력



  • try ~ catch 문 : 예외처리
		try {			//예외처리: I/O, Database, Network, Thread(다중처리)에서는 반드시 예외처리를 해야 함
			this.ex3();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

※throws Exception 사용하거나 try~catch를 씁니다. System.in.read() 메소드에 대한 예외 처리 코드이다. 

import java.io.IOException;


if(name.equals (irum)){T}
else{F}

name과 irum의 참조형 변수는 갖는 번지수가 다르기 때문에 ==로 비교하면 같지 않다고 나온다->F실행

그러므로 A equals(B)의 형태로 비교해야한다. (String타입)


package condition; public class StringTest { public void ex1(){ String name = "홍길동"; String irum = "홍길동"; System.out.println("==로 비교했을 때 * * *"); if (name == irum) { System.out.println("두 문자열이 동일합니다."); } else { System.out.println("두 문자열이 동일하지 않습니다."); } } public void ex2() { String name = "홍길동"; String irum = "홍길동"; System.out.println("equals로 비교했을 때 * * *"); if (name.equals(irum)) { System.out.println("두 문자열이 동일합니다."); }else { System.out.println("두 문자열이 동일하지 않습니다."); } } public StringTest(){ this.ex1(); //관계연산자(==)를 사용해 문자열을 비교했을 때 결과 this.ex2(); //equals를 사용해 문자열을 비교했을 때 결과 } public static void main(String[] args) { StringTest t = new StringTest(); } }

같은 번지수를 갖기 때문에 ex1과 ex2의 출력 결과는 동일하다.

두 문자열이 동일합니다. 라고 출력됩니다. 


package condition; public class StringTest { public void ex1(){ String name = new String("홍길동"); String irum = new String("홍길동"); System.out.println("==로 비교했을 때 * * *"); if (name == irum) { System.out.println("두 문자열이 동일합니다."); } else { System.out.println("두 문자열이 동일하지 않습니다."); } } public void ex2() { String name = new String("홍길동"); String irum = new String("홍길동"); System.out.println("equals로 비교했을 때 * * *"); if (name.equals(irum)) { System.out.println("두 문자열이 동일합니다."); }else { System.out.println("두 문자열이 동일하지 않습니다."); } } public StringTest(){ this.ex1(); //관계연산자(==)를 사용해 문자열을 비교했을 때 결과 this.ex2(); //equals를 사용해 문자열을 비교했을 때 결과 } public static void main(String[] args) { StringTest t = new StringTest(); } }


반면 위의 출력 결과는 서로 다릅니다. equals라는 메소드로 비교했을 때는 두 문자열을 동일하다고 출력합니다.



	public void exam04() {		//while문과 Math.random()함수를 사용해야 한다
		int sum = 0;
		
		while (sum != 5) {
			int rndm1 = (int)(Math.random()*6) + 1;
			int rndm2 = (int)(Math.random()*6) + 1;
			sum = rndm1+rndm2;
			System.out.printf("(%d, %d)\n", rndm1, rndm2);
		} 
	}

	public void exam05() {		//중첩 for문을 꼭 사용할 것 
		int x=0;
		int y=0;
		System.out.println("--- 	확인문제 05번	 ---");
		for ( x=1; x<11; x++) {
			for ( y=1; y<11; y++) {
				if ((4*x + 5*y) % 60 == 0)
				System.out.printf("(%d, %d)\n", x, y);
			}
		}
	}

	public void exam07() {
		boolean run = true;
		int balance = 0;
		int in = 0;
		int out = 0;
		Scanner sc = new Scanner(System.in);		
		while (run) {
			System.out.println("--------------------------------------");
			System.out.println(" 1. 예금 |  2. 출금  |  3. 잔고  |  4. 종료");
			System.out.println("--------------------------------------");
			System.out.print("선택: ");
			int num = sc.nextInt();			
			if (num == 1) {
				System.out.print("예금액> ");
				in = sc.nextInt();
				balance += in;
			} else if ( num == 2) {
				System.out.print("출금액> ");
				out = sc.nextInt();
				balance -= out;
			} else if ( num == 3) {
				System.out.printf("잔고> %d\n", balance);				
			} else if ( num == 4) {
				break;
			}			
		}
		sc.close();		//노랗게 경고 뜨는 것이 싫어서 닫았음.
		System.out.println("프로그램을 종료합니다. ");
	}

댓글