본문 바로가기
코딩/자바 기초특강

20.11.04 WED 자바 기초특강 3

by 6^6 2020. 11. 4.
728x90

<목차>

  1. 과제 최종 수정
  2. if문
  3. switch문
  4. for문
  5. while문
  6. for문 구구단
  7. while문 구구단
  8. do-whlie문 구구단
  9. 배열

 

 

 

 

1. 과제 최종 수정

★코딩을 직관적으로 바꿀 것. 보기 쉽게

class Ex10{

public static void main(String[] args){


int item1=3;
int item2=2;
int item3=4;
int item4=1;

int itemEa1=900;
int itemEa2=1200;
int itemEa3=300;
int itemEa4=2500;

int tot1=item1*itemEa1;
int tot2=item2*itemEa2;
int tot3=item3*itemEa3;
int tot4=item4*itemEa4;

int sum=tot1+tot2+tot3+tot4;

String msg="--------------------------------------\n";
msg+="비트편의점\n";
msg+="--------------------------------------\n";
msg+="상품 | 갯수 | 가격 | 계\n";
msg+="--------------------------------------\n";
msg+="생수 "+item1+" "+itemEa1+" "+tot1+"\n";
msg+="꼬깔콘 "+item2+" "+itemEa2+" "+tot2+"\n";
msg+="커피 "+item3+" "+itemEa3+" "+tot3+"\n";
msg+="아이스크림 "+item4+" "+itemEa4+" "+tot4+"\n";
msg+="--------------------------------------\n";
msg+=" 합계 "+sum+"\n";
msg+="--------------------------------------\n";


System.out.println(msg);

}
}

 

 

 

 

 

 

 

 

2. if문

class Ex11{

public static void main(String[] args){

//제어문(조건문(if문))
// if문(만약 00이라면 ~해라)
// if(boolean값){실행문;} //false면 실행x
// if(조건절){실행문;}
//


System.out.println("if문 하겠습니다"); // {}밖은 영향x

 


if(true){
System.out.println("true라서 실행함");

}

if(5>3){
System.out.println("true라서 실행함");

}

////////////////////////////////////
int su1 = 5;
if(su1>3){
System.out.println("true라서 실행함");

}

////////////////////////////////////////


int su2 = 5; //if-else문, else만 독단적사용불가.
if(su2>0){
System.out.println("양수입니다.");
}
else{ //if(su2<0)로 쓰면 실행을 안함. else(≒false)뒤엔 조건x
System.out.println("음수입니다.");
}

//////////////////////////////////////////////
int su3 = 0; //if-else if-else문은 결과값이 하나만 나온다.

if(su3>0){
System.out.println("0보다 큰 양수입니다.");
}
else if(su1<0){
System.out.println("0보다 작은 음수입니다.");
}
else{ // else if(true)와 같음.
System.out.println("0입니다.");
}


//////////////////////////////////////////////
System.out.println("수행했습니다."); // {}밖은 영향x


}

}

 

 

 

3. switch문

class Ex12{

public static void main(String[] args){

//제어문-조건문-switch문
/*
switch(값){
case 값:
실행문;
break;
case 값:
실행문;
break;
case 값:
실행문;
break;

default:
실행문;
}

*/
/*고려사항 2가지
1.default는 반드시 있어야하는건 아니다.
해당사항없으면 그냥 실행 안함.(=else역할).

2.수행할때 break가없으면
그 아래 해당하는 case값까지 다 실행되어버림.

*/

System.out.println("switch문 실행합니다.");

int su1=1;
switch(su1){
case 3: //case다음 나오는 값은 type을 똑같이 해야함. 아니면 오류남
System.out.println("3입니다");
break;

case 2:
System.out.println("2입니다");
break;

case 1:
System.out.println("1입니다");
break;

default:
System.out.println("1-3사이값이 아닙니다.");

}
System.out.println("switch문 실행했었습니다.");

//고려해야할 사항. 1.default는 반드시 들어가야하는게 아니

}

}

 

 

 

 

4. for문

class Ex13{

public static void main(String[] args){

//제어문-반복문-for문

/* for(초기화;조건문;증감식){
반복수행할 명령;
}

초기화→조건:true→수행
→증감식→조건:true→수행
→증감식→조건:true→수행
....
→증감식→조건:false→end

if ctrl+c 무한루프일때 빠져나올값

*/


System.out.println("출력");
System.out.println("출력");
System.out.println("출력");
System.out.println("출력");
System.out.println("출력");
/////////////////////////////////////////////
//아래 위 같은 결과값 나옴
/////////////////////////////////////////////

for(int i=0; i<5; i++){
System.out.println("출력");
}

//////////////활용///////////////////////////
int a=0; //왜있지..?필요x
for(int i=0; i<5; i++){

System.out.println("출력" +a++);
}
//////////////////////////////////////
int i=100;
for(i=0; i<5; i++){

System.out.println("출력" +i);
}

 

}
}

 

 

 

 

 

5. while문

class Ex15{

public static void main(String[] args){

//제어문 - 반복문 - while문
/*

while(조건-boolean데이터가 들어가야함){반복실행할 문장;}
조건 true일 동안 반복
-무한루트에 빠질 위험많음
*/

int su=0;
while(su<5){
//su=0;//무한루프
//System.out.println("무한");//무한루프
System.out.println("무한"+su++);//0~4까지 출력


}
}

}

 

 

 

 

6. for문 구구단

//구구단 7단 출력

class gugu{

public static void main(String[] args){

System.out.println("<구구단 7단>");


for(int i=1; i<10; i++){
System.out.println("7" + " * "+ (i) + " = "+(7*i));//여기에 i++를 썼더니 홀수로만 나옴

}

//먼저 아웃풋을 내고 반복적인걸 그다음 생각


////////a단 구구단 출력/////
int a = 8;
for(int i=1; i<10; i++){
System.out.println(a + " * "+ (i) + " = "+(7*i));//여기에 i++를 썼더니 홀수로만 나옴


}
}
}

 

 

 

 

7. while문 구구단

class guguwhile{

public static void main(String[] args){

System.out.println("");
int su=0;
while(su<9){
su++;
System.out.println("7" + "*" + su + "=" + 7*su);

 

}

}
}

 

 

8. do-while문

class Ex17{

public static void main(String[] args){
//제어문 - 반복문 - do-while문

/*
do{
반복할 문장;
}while(조건); // while조건뒤에 ;붙는것 주의

*/

int su=5;
do{ //처음부터 조건맞으면while문이랑 같은값으로 출력.안맞을것같으면 do-while문 만들기
System.out.println(su);
su--;
}while(su<0);
}
}

 

 

 

9.배열

class Ex18{

public static void main(String[] args){

int a1=1;
int a2=3;
int a3=5;
int a4=7;
int a5=9;

for(int i=0; i<5; i++){ //출력만 5번나옴
System.out.println("출력");}

for(int i=1; i<10; i+=2){ //1,3,5,7,9출력
System.out.println("출력"+i);}

//출력 뒤에 1,3,5,7,9 출력하기-내방법
for(int i=0; i<5; i++){
System.out.println("출력"+(i*2+1));}

//선생님방법
int temp=1;
for(int i=0; i<5; i++){
System.out.println("출력"+temp);
temp++;
temp++;}


System.out.println("-------------------------");
for(int i=0; i<5; i++){
System.out.println(("a"+(i+1))+"="+(i*2+1));}
//이렇게하면 a값이 나오지않고 a1,a2이런식으로 나옴

System.out.println("-----그걸가능하게 해주는것=배열--------------");
//배열원리(java랑 살짝다름)배열에서의 인덱스는 0부터 시작. 몇번뛰어넘는지 알려주는거여서 5개 인덱스있으면 4번까지 가능
int[] su=new int[5];//5 안넣어주면 오류.
su[0]=2;
su[1]=4;
su[2]=6;
su[3]=8;
su[4]=10;
for(int i=0; i<5; i++){
System.out.println(su[i]);}


}
}

<심화예제>

class Ex19{

public static void main(String[] args){

char[] arr1=new char[3];
arr1[0]='A';
arr1[1]='B';
arr1[2]='C';

for(int i=0; i<3; i++){
System.out.println(arr1[i]);
}

char[] arr2=new char[]{'a','b','c'};//=new char[3]{'a','b','c'}; 이렇게쓰면오류. 이미 abc3개가 있어서
for(int i=0; i<3; i++){
System.out.println(arr2[i]);}

char[] arr3={'x','y','z'};
for(int i=0; i<3; i++){ //이렇게쓰면 복잡해질때 잘못하면 런타임오류날수있음.
System.out.println(arr3[i]);}

System.out.println(arr3.length); //length는 크기함수 6개들어가면 6출력

}

}

<심화 예제>

class Ex20{

public static void main(String[] args){
System.out.println("총 "+args.length+"개"); //출력창에 문자넣으면 크기 나옴

for(int i=0; i System.out.println(i+1+"번째"+":"+args[i]);

}
}
}

<심화 예제>

class Ex21{

public static void main(String[] args){

/* int[] arr1={1,3,5,7};
int[] arr2={1,3,5,7};
int[] arr3=arr1;//배열의 옅은복사-프로그래밍하면서 많이 씀

arr3[0]=0;

System.out.println(arr1==arr2); //false나옴. 값을 확보한 위치 자체가 달라서....
System.out.println(arr3==arr1); //true나옴

for(int i=0; i System.out.println(arr1[i]); } //arr1이 0부터 출력됨. arr3이 0으로 초기화시켜서.
//이걸 이해하지 못하면 객채지향을 할수없음

*/

////////////////////////////////////////
int[] arr1={1,3,5,7};
int[] arr2={1,3,5,7};

int[] arr4=new int[4];
for(int i=0; i arr4[i]=arr1[i]; //배열의 깊은복사
}

arr4[0]=10; //이걸 빼줘도 넣어줘도 arr1의 첫번째 값은 변하지않는다.

System.out.println(arr1==arr2); //false나옴. 값을 확보한 위치 자체가 달라서....

for(int i=0; i System.out.println(arr1[i]); } //arr1이 0부터 출력됨. arr3이 0으로 초기화시켜서.
//이걸 이해하지 못하면 객채지향을 할수없음


}
}

728x90

댓글