오픈프레임워크_Day04
### : 목차 구분 기호
--- : 목차 내에 항목 구분 기호
목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드
4. 과제
###################################
1. 이론 및 정보
-----------------------------------
스킬 인벤토리
- ANT
-----------------------------------
제어문(Control Statement)
이어서
1. 종류
1) 조건문
- if
- switch
2) 반복문 ( 반복횟수, 무한루프 두가지를 잘 체크하라 )
- while
- do while
- for
* 반복문에서 잘 지켜야할 것 세가지
- 초기화
- 조건
- 카운터
2. if
.
.
중략
.
.
3) if(조건식){ //다중 if문
문장;
...
}else if(조건식){
문장;
...
}else if(조건식){
문장;
...
}[else { //대괄호는 생략가능
문장;
...
}]
4) nested if (포개져있는 상태, 안써도 됨)
3. switch
1) 문법
switch(변수, 수식){ //괄호 안에는 값이 들어감
case 값:
문장;
case 값:
문장;
...
[default:
문장;] //대괄호는 생략가능
}
break;는 switch에 있는 문법이 아님
다른 곳에 있는 것을 가져다 씀
불편한점
- 변수와 값이 같은 경우만 처리함!! 같다라는 조건
- 변수는 반드시 정수형만 가능, 실수 안됨
4. while
1) 문법
while(조건식){ //괄호 안의 조건이 참인 동안
문장;
...
}
5. do while
1) 문법
do{
문장;
...
}while(조건식){
문장;
...
}
6. for
1) 문법
for(초기화;조건식;카운터){
문장;
...
}
7. 반복문 강제 종료 (반드시 반복문안에서만 써야함)
1) continue : 일시 종료 보다는 일시 정지(반복문의 맨 밑으로 보냄)
2) break : 완전 종료 (예외적으로 break만 다른 곳에서도 쓰임)
8. nested loop(중첩 반복문)
-----------------------------------
C/C++ 돌아가는 원리 와 JVM(Java Virtual Machine) 설명
프로그램이 실행되기 위해서는 운영체제의 제공을 받아야 한다.
운영체제와 프로그램사이에서 교량역할을 하는 언어를 통해 운영체제의 도움을 받는다.
이때 사용하는 것이 운영체제 API이다.
그래서 C/C++은 이러한 교량역할을 해서 운영체제에서 직접 프로그램을 실행한다.
하지만 C/C++은 Window에서 컴파일 하면 Window에서만 가능하고
리눅스 에서 컴파일하면 리눅스에서만 가능하다.
이유는 위에 말한 것처럼 해당 운영체제의 API를 사용해야하기 때문이다.
JAVA는 이와는 다른 방식으로 운용된다.
JAVA는 운영체제에서 따로 메모리 영역을 새로 구성하여 JAVA 프로그램을 실행시킨다
이 영역을 JVM이라고 하고, 이때 윈도우에서 javac.exe는 java로 만든 소스 코드를
JVM 영역에서 컴파일 한다.
JVM에서 컴파일이 되면 JVM이 이해할 수 있는 바이트코드가 생성된다.
이 바이트코드를 java.exe가 JVM에서 실행시켜주는 역할을 한다.
JVM은 위와 같은 방식으로 운영체제에 상관없이 동작할 수 있도록 제공되며
이는 C/C++과는 다르게 플랫폼에 의존적이지 않게 해준다.
-----------------------------------
CLASS
1. 묶음
- 변수, 메서드
2. 최소 단위(캡슐화 단위)
3. 설계도(추상적)//가장 중요함!
- 인스턴스(Instance 클래스를 가지고 실제로 만든 것, 메모리 공간, 실제 메모리 데이터, 메모리 복사본)
4. 사용자 정의 데이터타입(User Define DataType)
1) Built-in DataType(비 객체형 데이터 타입 - 반드시 값만 저장, 메모리 주소 저장X)
byte short int long double float char boolean
2) User Define DataType(객체형 데이터 타입 - 반드시 주소만 저장)
class로 구현
###################################
2. 설정 및 그 밖에
-----------------------------------
https://www.eclipse.org/downloads
Eclipse IDE for Java EE Developers 32bit 받음
eclipse-jee-luna-SR2-win32.zip
###################################
3. 소스코드
-----------------------------------
public class IfTest2{
public static void main(String[] args) throws java.io.IOException {
/*
int avg = 75;
char hak = ' ';
if(avg >= 90)
hak = 'A';
else if(avg >= 80)
hak = 'B';
else if(avg >= 70)
hak = 'C';
else if(avg >= 60)
hak = 'D';
else
hak = 'F';
System.out.println("당신의 학점은 "+hak+ " 입니다");
*/
/*
int num1 = 10, num2 = 7, num3 = 8;
if( num1 > num2 ){
if( num1 > num3 )
System.out.println("num1이 가장 큰 값입니다.");
else
System.out.println("num3이 가장 큰 값입니다.");
}else{
if( num2 > num3 )
System.out.println("num2이 가장 큰 값입니다.");
else
System.out.println("num3이 가장 큰 값입니다.");
}
*/
//올바른 id : i , 패스워드 : x
System.out.print("아이디 : ");
char id = (char)System.in.read();
System.in.skip(2);
System.out.print("패스워드 : ");
char pw = (char)System.in.read();
if( id != 'i' ){
if( pw != 'x' )
System.out.println("아이디와 패스워드 모두 틀렸습니다.");
else
System.out.println("아이디는 틀렸고 패스워드는 맞았습니다.");
}else{
if( pw != 'x' )
System.out.println("아이디는 맞았고 패스워드는 틀렸습니다.");
else
System.out.println("아이디와 패스워드 모두 맞았습니다.");
}
}
}
-----------------------------------
public class SwitchTest1{
public static void main(String[] args) {
/*
int data = 3;
switch(data){
case 1:
System.out.println("사과");
break;
case 2:
System.out.println("배");
break;
case 3:
System.out.println("포도");
break;
default:
System.out.println("해당 과일이 없다");
}
*/
int avg = 75;
switch(avg / 10){
case 10:
System.out.println("A");
break;
case 9:
System.out.println("B");
break;
case 8:
System.out.println("C");
break;
case 7:
System.out.println("D");
break;
case 6:
System.out.println("E");
break;
default
System.out.println("F");
}
}
}
-----------------------------------
public class WhileTest1{
public static void main(String[] args) {
// *********
/*
int cnt = 0;
while(cnt < 10){
System.out.print("*");
cnt++;
}
*/
// 1부터 10까지의 합계 : 55
/*
int cnt = 0, sum = 0;
while( cnt < 11 ){
sum += cnt;
cnt++;
}
System.out.println(sum);
*/
//2의 10승을 구하라. : 1024
int cnt = 0, sum = 1;
while( cnt < 10 ){
sum *= 2;
cnt++;
}
System.out.println(sum);
}
}
-----------------------------------
public class ForTest1{
public static void main(String[] args) {
/*
for(int cnt = 0; cnt < 10; cnt++){
System.out.print("*");
}
*/
/*
int cnt = 0;
for(; cnt < 10; cnt++){
System.out.print("*");
}
*/
/*
int cnt = 0;
for(; cnt < 10;){
System.out.print("*");
cnt++;
}
*/
//무한반복
/*
for(;;){
}
*/
//무한반복
/*
while(ture){
}
*/
//아래의 답을 예상하시오 : 15, 순서를 제대로 파악하라
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/*
//data의 범위를 생각해서 선언한 예제
int data;
for(data = 0; data<8; data++){
data += data;
}
System.out.println(data);
*/
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
for(int i = 0; i<10; i++){
if( i == 5 )
//continue;
break;
System.out.println(i);
}
}
}
-----------------------------------
public class NestedLoop {
public static void main(String[] args) {
/*
**********
**********
**********
*/
for(int i = 0; i<3; i++){
for(int j = 0; j<10; j++){
System.out.print("*");
}
System.out.println();
}
/*
*
**
***
****
*****
*/
for(int i = 0; i<5; i++){
for(int j = 0; j<=i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
-----------------------------------
class ClassDemo1{
int i;
double d;
}//클래스
public class ClassTest1{
public static void main(String[] args) {
ClassDemo1 obj = new ClassDemo1();//인스턴스 생성, 12byte
//obj 는 참조변수, 주소를 저장
obj.i = 10;
obj.d = 3.14;
System.out.println(obj.i+", "+obj.d);
ClassDemo1 obj1 = new ClassDemo1();
obj1.i = 20;
obj1.d = 3.5;
System.out.println(obj1.i+", "+obj1.d);
ClassDemo1 obj2 = obj1;
obj2.i = 200;
System.out.println(obj1.i);
obj = obj2;
System.out.println(obj.i);
}
}
-----------------------------------
###################################
4. 과제 (이번주 주말까지)
-----------------------------------
1. 입력받은 값이 소문자이면 소문자라고 출력하고
대문자이면 대문자라고 출력하고 숫자이면 숫자라고 출력하고
그외의 데이터는 기타라고 출력한다.
public class Problem{
public static void main(String[] args) throws java.io.IOException {
System.out.print("입력하시오 : ");
int value = System.in.read();
if( (value >= 'a') && (value <= 'z') )
System.out.println("소문자");
else if( (value >= 'A') && (value <= 'Z') )
System.out.println("대문자");
else if( (value >= '0') && (value <= '9') )
System.out.println("숫자");
else
System.out.println("기타");
}
}
-----------------------------------
2. Simple Calculation(사칙 연산만 구성)
예)
연산자 : +
숫자1 : 2
숫자2 : 3
결과 : 2 + 3 = 5
재실행시
연산자 : *
숫자1 : 2
숫자2 : 3
결과 : 2 * 3 = 6
public class Problem{
public static void main(String[] args) throws java.io.IOException {
System.out.print("연산자 : ");
int operator = System.in.read();
System.in.skip(2);
System.out.print("숫자1 : ");
int num1 = System.in.read() - 48;
System.in.skip(2);
System.out.print("숫자2 : ");
int num2 = System.in.read() - 48;
System.in.skip(2);
if( operator == '+' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1+num2));
else if( operator == '-' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1-num2));
else if( operator == '*' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1*num2));
else if( operator == '/' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1/num2));
}
}
-----------------------------------
3. 계산기 프로그램을 개선하자.
예)
연산자 : +
숫자1 : 2
숫자2 : 3
결과 : 2 + 3 = 5
//2번과 다르게 계속 실행
연산자 : *
숫자1 : 2
숫자2 : 3
결과 : 2 * 3 = 6
연산자 : q나 x를 입력하면 종료~
public class Problem{
public static void main(String[] args) throws java.io.IOException {
while(true){
System.out.print("연산자 : ");
int operator = System.in.read();
System.in.skip(2);
if( ((char)operator == 'q') || ((char)operator == 'x') )
break;
System.out.print("숫자1 : ");
int num1 = System.in.read() - 48;
System.in.skip(2);
System.out.print("숫자2 : ");
int num2 = System.in.read() - 48;
System.in.skip(2);
if( operator == '+' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1+num2));
else if( operator == '-' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1-num2));
else if( operator == '*' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1*num2));
else if( operator == '/' )
System.out.println("결과 : "+num1+" "+(char)operator+" "+num2+" = "+(num1/num2));
System.out.println();
}
}
}
-----------------------------------
4. 갤런을 리터로 바꿔보자
- 100갤런까지 변환표를 만들자
- 단 10갤런마다 공백을 주도록 하자
1갤런 ?리터
2갤런 ?리터
3갤런 ?리터
...
public class Problem{
public static void main(String[] args) {
for(int i = 1; i<=100; i++){
System.out.println("\t"+i+"갤런"+"\t"+(i*3.785412)+"리터");
if( (i % 10) == 0 ){
System.out.println();
}
}
}
}
-----------------------------------
5. 숫자를 입력받아 합계를 구하는 예제
- 0을 입력하면 종료
1
2
5
4
0
합계 : ?
public class Problem{
public static void main(String[] args) throws java.io.IOException {
int num = 0, temp = 0;
while(true){
System.out.print("input number : ");
temp = System.in.read() - 48;
System.in.skip(2);
if( temp == 0 )
break;
num += temp;
}
System.out.print("합계 : "+num);
}
}
-----------------------------------
6. 숫자를 입력받아 짝수와 홀수의 합을 구하라.
단, 입력 횟수를 입력받도록 하자.
입력 횟수 : 3
2
5
3
짝수의 합 : 2
홀수의 합 : 8
public class Problem{
public static void main(String[] args) throws java.io.IOException {
int cnt = 0, even = 0, odd = 0, temp = 0;
System.out.print("입력 횟수 : ");
cnt = System.in.read() - 48;
System.in.skip(2);
for(; cnt>0; cnt--){
System.out.print("input number : ");
temp = System.in.read() - 48;
System.in.skip(2);
if( (temp % 2) == 0 )
even += temp;
else
odd += temp;
}
System.out.println("짝수의 합: "+even);
System.out.println("홀수의 합: "+odd);
}
}
-----------------------------------
7. 간단한 문자 추측 게임 프로그램을 만들자
미리 정답을 정해 놓는다. (예를 들면 k)
a ~ z 까지 생각한 문자 입력 : b
틀렸다
a ~ z 까지 생각한 문자 입력 : c
틀렸다
a ~ z 까지 생각한 문자 입력 : z
틀렸다
....
a ~ z 까지 생각한 문자 입력 : k
시도 횟수 : ? 만에 맞았습니다 축하합니다.
//문자에 가까워지면 가까워지고 있다는 메세지 추가 가능
public class Problem{
public static void main(String[] args) throws java.io.IOException {
char answer = 'z', input;
int cnt = 0, distance = 5;
while(true){
System.out.print("a ~ z 까지 생각한 문자 입력 : ");
input = (char)System.in.read();
System.in.skip(2);
cnt++;
if( answer > input ){
if((answer - input) < distance){
System.out.print("가까이 있습니다");
}else{
System.out.print("멀리 있습니다");
}
}
else if( answer < input ){
if((input - answer) < distance){
System.out.print("가까이 있습니다");
}else{
System.out.print("멀리 있습니다");
}
}
else{
System.out.println("시도 횟수 : "+cnt+" 만에 맞았습니다");
break;
}
System.out.println();
}
}
}
-----------------------------------
8. 간단한 도움말 시스템을 만들자
1. if
2. switch
3. 특정 용어 가능
4. for
...
번호 선택 : 1
if(조건식)
문장;
1. if
2. switch
3. 특정 용어 가능
4. for
...
번호 선택 : 2
while(조건식)
문장;
... 반복
1. if
2. switch
3. 특정 용어 가능
4. for
...
번호 선택 : 0
종료
public class Problem{
public static void main(String[] args) throws java.io.IOException {
boolean s1 = true;
int num = 0;
while(s1){
System.out.println("0. 프로그램 종료");
System.out.println("1. if");
System.out.println("2. switch");
System.out.println("3. while");
System.out.println("4. do while");
System.out.println("5. for");
System.out.print("번호 선택 : ");
num = System.in.read() - 48;
System.in.skip(2);
System.out.println();
switch(num){
case 0:
s1 = false;
break;
case 1:
System.out.println("if(조건식){ \n"
+" 문장;\n"
+" ...\n"
+"}else if(조건식){ \n"
+" 문장;\n"
+" ...\n"
+"}else if(조건식){ \n"
+" 문장;\n"
+" ...\n"
+"}[else {\n"
+" 문장;\n"
+" ...\n"
+"}]\n"
);
break;
case 2:
System.out.println("switch(변수, 수식){\n"
+" case 값:\n"
+" 문장;\n"
+" case 값:\n"
+" 문장;\n"
+" case 값:\n"
+"...\n"
+"[default:\n"
+" 문장;]\n"
+"}\n"
);
break;
case 3:
System.out.println("while(조건식){\n"
+" 문장;\n"
+" ...\n"
+"}\n"
);
break;
case 4:
System.out.println("do{\n"
+" 문장;\n"
+" ...\n"
+"}while(조건식){\n"
+" 문장;\n"
+" ...\n"
+"}\n"
);
break;
case 5:
System.out.println("for(초기화;조건식;카운터){\n"
+" 문장;\n"
+" ...\n"
+"}\n"
+"for 반복문의 순서!\n"
+"1. 초기화\n"
+"2. 조건식\n"
+"3. 수행될 문장\n"
+"4. 증감식\n"
+"2. 조건식\n"
+"3. 수행될 문장\n"
+"4. 증감식\n"
+"이런식으로 조건이 false 가 될 때까지 2->3->4 반복\n"
);
break;
default:
break;
}
}
}
}
-----------------------------------
9. "." 입력이 될때 까지 키보드 입력을 읽어서
입력된 공백의 갯수를 세고 그 합을 출력하시오.
public class Problem{
public static void main(String[] args) throws java.io.IOException {
int input = 0, cnt = 0;
while(true){
input = System.in.read();
if( input == '.' )
break;
else if( input == ' ' )
cnt++;
}
System.out.println("공백의 갯수는 "+cnt);
}
}
-----------------------------------
프린트 하기
/*
*
***
*****
*******
*********
*/
public class Problem{
public static void main(String[] args) {
int t = 0;
for(int i = 5; i > 0; i--){
for(int j = 9; j > 0; j--){
if( (j > (i+t)) || (i > j) )
System.out.print(" ");
else
System.out.print("*");
}
t += 2;
System.out.println();
}
}
}
-----------------------------------
프린트 하기
/*
*
***
*****
*******
*********
*******
*****
***
*
*/
public class Problem{
public static void main(String[] args) {
int t = 0;
for(int i = 5; i > 0; i--){
for(int j = 9; j > 0; j--){
if( (j > (i+t)) || (i > j) )
System.out.print(" ");
else
System.out.print("*");
}
t += 2;
System.out.println();
}
t = 8;
for(int i = 1; i < 5; i++){
for(int j = 9; j > 0; j--){
if( (j < (i+t)) && (i < j) )
System.out.print("*");
else
System.out.print(" ");
}
t -= 2;
System.out.println();
}
}
}
-----------------------------------
프린트 하기
/*
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
*/
public class Problem{
public static void main(String[] args) {
int i = 1;
while(i <= 21){
System.out.print(i+"\t");
if( (i % 7) == 0 )
System.out.println();
i++;
}
}
}
-----------------------------------
-----------------------------------
###################################
-----------------------------------