날아라쩡글이의 블로그입니다.

파일정리04(2021-08-12) 본문

java (학원 전) story/java 코딩 파일 정리

파일정리04(2021-08-12)

날아라쩡글이 2021. 8. 12. 17:12
반응형


/*1.while(조건)
2.do..while(조건)
활용 및 차이점을 확인 */ 
public class whileTest{
//main method
public static void main(String[]args){
//while문 


int i = 0;// 1.순환문의 조건을 주기위한 int i 초기화
while(i<10){//2.조건 (boolean data type)
// while (0){//==>compile error (error를 확인하면..)
System.out.println("여기는 while문 내부안임 i="+i);
i++;//3.증감식
}


//do while문 
int j = 0;
//순환식의 조건을 만들어 주기위한 int = j 초기화 진행
do{System.out.println("\n\t 여기는 do문 내부임 j ="+j);//do의 경우 최소 한번은 실행되는 값
j++;//2.증감식 입력
}while(j<1);//3.조건 (boolean data type) j가 1보다 작으면 수행되는 식을 입력
System.out.println("\n==================\n");
//while문을 이용하여 5단을 출력하는 프로그램

int k = 1;
while (k<10){//k가 10보다 작을 경우 
System.out.println("5*"+k+"="+5*k); //k과 5를 곱해서 출력함 
k++;//k의 증감
}

//무한 루프와 무한루프후단의 실행문에서 compile error 이해 
//while (true){
//System.out.println("여기는 반복문 내부의 무한루프");
//}

//아래의 주석을 풀면 compile error가 발생한다. 이유는.. 
System.out.println("error가 발생한다. 이유는..");//무한루프를 해제하고 진행할 수가 없어서 compile error가

발생함


}//end of main

}//end of class

 




/*1.for (초기값;조건;변화값)문을 이용
2.while(조건)문과 do while(조건)과의 차이점 및 활용확인*/

public class ForTest{

public static void main(String[]args){


int j =5;
System.out.println(j+"단을 출력합니다.");

//for문은 while문과 달리 초기화를 for문 내부에서 한다. 
for(int i =1; i<10; i++){//생성자; j가 10보다 작을때까지; i의 값이 증가된다
System.out.println(j+"*"+i+"="+j*i);//j의경우 5 i의 경우 0~9까지의 숫자가 작성이 된다. 5단의 계산식이 나온다. 
}

//반복문 for/while사용과 차이점의 이해 
int k = 1;
while (k<10){
System.out.println("5*"+k+"="+5*k);
k++;
}//k가 10미만일경우 true 총 1~9번을 반복한다.
int i = 0;
System.out.println("i의 최종변경 값:"+i);//9의 값
System.out.println("k의 최종변경 값:"+k); //10의 값


//무한 루프와 무한루프후단의 실행문에서 compile error를 이해하면..
for ( ; ; ){
System.out.println("여기는 반복문내부의 무한 루프");
}

//아래의 주석을 풀면compile error가 발생한다.. 이유는 
//System.out.println("error가 발생한다. 이유는...");//무한루프를 해제하고 진행할 수가 없어서 compile error가

발생함

}//end of main

}//end of class

while문과 for문의 초기값 조건식, 증감식의 경우 동일한 버젼임

while위에 초기값 (증감식) 조건식 for의 경우 (초기값; 조건식; 증감식)



/*
keyword : break ==>자신이 속한 반복문은 종료


break문을 이용하여 입력받은 수까지의 합을 구하자 
==>1~10더해서 55를 출력
*/

public class BreakTest{
public static void main(String[]args){


//입력받은 data를 int 변환

int inputData = Integer.parseInt(args[0]);

//while문의 초기화 변수 선언

int i = 0;
//더한 값을 저장하기 위한 변수 선언

int sum =0;

while(true){//while문의 반복
i++;//i의 1씩 증감
sum=sum+i;//sum에 i의 총합계를 나타냄

if (i == inputData){ //만약 i와 inputData가 동일해지면 
break;}//끝

}
System.out.println("0~"+inputData+"까지의 합은 :"+sum+"입니다");
}
}

 

continue = 중단후 다시돌아와서 반복(하부수행 중지 반복)

break = 자신을 중지 




/*continue 이후 실행문을 중단, 다시 반복문 실행


continue문을 이용하여 짝수만 더하기 */

public class ContinueTest{

public static void main(String[]args){

//입력받은 data를 int변환

int inputData = Integer.parseInt(args[0]);

//더한 값을 저장하기 위한 변수 

int sum = 0;

for(int i=0;i<inputData; i++){
if(i%2==1){//홀수라면 
continue;//바로 중단후 다시 for문으로 돌아감
}//if문을 벗어났기 때문에 
sum = sum+i;//sum 과 짝수의 i가 더해짐
//아래의 내용은 for순환문을 수행하며
//변수의 값의 변화를 확인하기 위한 출력문
//System.out.println("i:"+i);
//System.out.println("sum:"+sum);
}
System.out.println("0~"+inputData+"까지의 짝수의 합은:" +sum);
}
}

 


public class ArrayTest01{

public static void main(String[]args){
int[] intArray = new int[9];
1.int DataType Array의 선언 int[]intArray or int int Array[];intArray[0]=1;

2.9개의 int를 저장할 수 있는 공간을 갖는 array를 생성 , 대입연산자를 이용해서 위치정보를 대입하고 

3.array 선언과 생성 위치정보대입을 동시에 진행

int []intArray = new int(9);

4.각각의 방에 value대입
intArray[1]=2;
intArray[2]=3;
intArray[3]=4;
intArray[4]=5;
intArray[5]=6;
intArray[6]=7;
intArray[7]=8;
intArray[8]=9;


for(int j = 0; j<9;j++){
System.out.println("intArray["+j+"]의 값은:"+ intArray[j]);
}
//각각의 값을 이용하여 5단을 출력
System.out.println("------------------------");
for(int k = 0; k< intArray.length;k++){//최종길이를 모르기 때문에 .length를 작성함
System.out.println("5X"+intArray[k]+"="+5*intArray[k]);
}

//아래의 출력내용을 이해하자 
int[]j = intArray;
System.out.println("==================");
System.out.println("j[0]의 값은:"+j[0]);
System.out.println("j[5]의 값은:"+j[5]);

//위의 내용을 이해했다면 주석을 풀고 확인 
j[5] = 1234;
//System.out.println("j[5]==>"+j[5]);
//System.out.println("i[5]==>"+intArray[5];
}
}

기본형의 경우 쓰기가 편하지만 갯수가 많이 필요함, 관리가 어려움

그렇기 때문에 어떤 변수에 Array를 호출할 수 있는 참조형을 넣어 위치정보만 집어넣고 입력함

 

반응형
Comments