My footsteps

code 실습 / 6️⃣ 본문

국비수업/CODE

code 실습 / 6️⃣

밀김 2023. 2. 23. 16:09
728x90

 



변수

✏️ 1byte 정수형 변수 num 선언 후 값 100을 할당하세요

✏️ 2byte 정수형 변수 num1 선언 후 값 1000을 할당하세요

✏️ 4byte 정수형 변수 num2 선언 후 값 10000을 할당하세요

✏️ 8byte 정수형 변수 num3 선언 후 값 1000000000을 할당하세요

✏️printf를 사용하여 “현재 출력되는 값은 정수형이며 값은 10000입니다"를 출력해보세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
    
		int num = 100;
		int num1 = 1000;
		int num2 = 10000;
		int num3 = 1000000000;
		
		System.out.printf("현재 출력되는 값은 정수형이며 %d 입니다.",num2);
	}
}

 

 

 

✏️ 정수 자료형 중 가장 많이 사용되는 자료형은 어떤 것인가요?

int형 

 

 

✏️ 8byte 실수형 변수 d 선언 후 값  1.23456789를 할당하세요

✏️ printf를 사용하여 “현재 출력되는 값은 실수형이며 값은 1.23456789입니다"를 출력해보세요

public class Homework {
	public static void main(String[] args) {
    
		float d = 1.23456789f;
		System.out.printf("현재 출력되는 값은 실수형이며 값은 %f 입니다.",d);

	}
}

 

✏️ 실수 자료형 중 가장 많이 사용되는 자료형은 어떤 것인가요?

double 입니돳

 

✏️ 문자형 변수 c를 선언 후 문자 ‘a’를 할당하세요

✏️ printf를 사용하여 “현재 출력되는 숫자는 문자형이며 값은 a입니다"를 출력해보세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
    
		char c = 'a';
		System.out.printf("현재 출력되는 숫자는 문자형이며 %c 입니다", c);
        
	}
}

 

 

✏️ 논리형 변수 flag를 선언 후 false를 할당하세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
    
		boolean flag = false;
        
	}
}

 

 

 

 

 

조건문

✏️ 1부터 30까지의 숫자 중 반복문과 조건문을 사용하여 4의 배수만 출력되도록 코드를 작성하세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
    
		for(int i=0; i<31; i++) {
			if(i%4==0)
			System.out.println(i);
            
		}
	}
}

 

 

 

✏️ 1부터 30까지의 숫자 중 2의 배수이면서 3의 배수인 값만  출력해보세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
    
		for(int i=0; i<31; i++) {
			if(i%2==0 && i%3==0)
			System.out.println(i);
            
		}
	}
}

 

 

✏️ 1부터 30까지의 숫자 중 3의 배수이거나 7의 배수인 값을  하나의 조건문을 사용하여 출력해보세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {

		for(int i=1; i<=30; i++) {
			if(i%3==0 || i%7==0)
				System.out.println(i);
		}		
}			
}

 

 

 

✏️ Scanner를 이용하여 사용자에게 숫자를 입력받고 입력받은 숫자가 20보다 크면서 50보다 작으면 입력받은 숫자를 출력하세요

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		if(20<num && num<50)
			System.out.println(num);
}
}

 

 

✏️ 정수형 변수에 값 50을 할당하고 Scanner를 이용하여 사용자에게 숫자를 입력받아 입력받은 숫자가 선언된 변수의 값보다 크면 “50보다 큰 숫자입니다" 50보다 작으면 “50보다 작은 숫자입니다”를 출력하세요

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
		
		int i = 50;
		
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		if(num>i)
			System.out.println("50보다 큰 숫자입니다");
		else if(num<i)
			System.out.println("50보다 작은 숫자입니다");
}
}

 

 

 

✏️ Scanner를 이용하여 사용자에게 1과 5 사이의 숫자를 입력받고 입력받은 숫자가 1이면 “떡볶이", 2이면 “어묵", 3이면 “순대”, 4이면 “튀김”, 5이면 “핫도그”를 출력하고 만일 1과 5 사이의 숫자가 아닐 경우 “유효한 숫자의 범위가 아닙니다"라는 메시지를 출력하는 코드를 작성하세요

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		if(num==1)
			System.out.println("떡볶쓰");
		else if(num==2)
			System.out.println("오뎅쓰");
		if(num==3)
			System.out.println("순대쓰");
		else if(num==4)
			System.out.println("튀김쓰");
		if(num==5)
			System.out.println("햣도그");
		else if(num>6)
			System.out.println("유효한 숫자의 범위가 아닙니다.");
		
}
}

 

 

 

✏️ Scanner를 이용하여 사용자에게 점수를 입력받고 입력받은 점수가 90점 이상이면 ‘A’, 80점 이상이면 ‘B’, 70점 이상이면 ‘C’, 60점 이상이면 ‘D’를 출력하고 그 외의 점수는 ‘F’를 출력하는 코드를 작성하세요

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		if(90<=num)
			System.out.println("A");
		else if(80<=num)
			System.out.println("B");
		else if(70<=num)
			System.out.println("C");
		else if(60<=num)
			System.out.println("D");
		else if(50>=num)
			System.out.println("F");
		
}
}

 

 

 

 

 

 

반복문 

✏️ for문과 while문을 사용하는 기준을 설명해보세요

for문은 조건식을 종료할 기준을 명확히 정해두고 실행시킨다면 while문은 정확한 조건보다는 유동적인 조건식일때 사용한다 

 

 

✏️ for문의 각 ?를 설명해보세요 for ( ? ; ?; ?) { }

초깃값,조건,증감식

 

 

✏️ for문과 int i를 이용해서 10번 반복되는 for문을 작성해보세요. { } 안에는 비워주세요.

package ex3.control;
public class Homework {
	public static void main(String[] args) {

		for(int i=0; i<10; i++) {
}
}
}

 

 

✏️ for문을 이용해서, 콘솔에 문자열 “Hello newlecture!”를 3번 출력해보세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {

		for(int i=0; i<3; i++) {
			System.out.println("Hello newlecture!");
}	
}
}

 

 

 

✏️ num이 짝수일때만 콘솔에 문자열 “Hello newlecture!”를 출력해보세요.

int num = 0;

for ( ? ; i < 10; ?) {

  num++;

}

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int num = 0;
		for(int i =2; i<10; i++) {
			num++;
			if(i%2==0) //2 4 6 8 
				System.out.println("Hello newlecture!");
		}	
}			
}

 

 

 

✏️ for문을 이용해서, 콘솔에 정수 1부터 10까지 출력해보세요. 

for(int i = 0; ? ; ? ) { }

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		for(int i=0; i<10; i++) {
			System.out.println(i+1); 
		}
}
}

 

 

 

✏️ for문을 이용해서, 콘솔에 정수 10부터 1까지 출력해보세요. 

for(int num = 10; ?; ?) {}

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		for(int i=10; i>=1; i--) {
			System.out.println(i);
		}
}
}

 

 

 

✏️ for문을 이용해서, 콘솔에 정수 1부터 10까지 짝수만 출력해보세요.

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		for(int i=0; i<=10; i++) {
			if(i%2==0)
			System.out.println(i);
		}	
}
}

 

 

 

✏️ for문을 이용해서, 콘솔에 정수 1부터 10까지 홀수만 더한 값을 출력해보세요. 

int sum = 0;

for(int i = 0; ? ; ? ) { }

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int sum = 0;
		
		for(int i=0; i<=10; i++) {
			if(i%2==1)
			sum = sum+i;
			System.out.println(sum);
		}
}
}

 

 

 

✏️ while문의 ?를 설명해보세요 while( ? ) { }

반복하고싶은 조건문

 

 

 

✏️ number가 100이 되면, while문을 종료하는 코드를 작성하세요.

int number = 0;

while(true) {

 number++;

}

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int number = 0;
		while(true) {
			number++;
			if(number==100) {
				System.out.println(number);
				System.out.println("종료");
			}//if끝
		}//while끝
}			
}

 

 

 

✏️  while문을 이용하여, number가 짝수마다 +가 출력되고, 홀수마다 -가 출력되는 코드를 작성해보세요.

number의 값이 100이 되면 while문을 빠져나와주세요.

-+-+-+-+-+... (반복)

package ex3.control;
public class Newclass1 {
	public static void main(String[] args) {
    
		int number=0;
		
		while(100>=number) {
			if(number%2==0)
				System.out.print("+");
			else
				System.out.print("-");
			number++;
		}//while끝
		}
	}

 

 

✏️  while문을 이용하여, number가 짝수마다 +가 출력되고, 홀수마다 -가 출력되는 코드를 작성해보세요. 이때, x축의 문자가 10개가 되면, 줄바꿈을 해보세요

-+-+-+-+-+

-+-+-+-+-+

-+-+-+-+-+

…(반복)

package ex3.control;
public class Newclass1 {
	public static void main(String[] args) {

		int number=0;
		
		while(100>=number) {
			if(number%2==0)
				System.out.print("+");
			else
				System.out.print("-");
			number++;
			if(number%10==0)
				System.out.println();
		}//while끝
	
		}
	}

 

 

✏️for문을 이용하여  다음처럼 구구단 표를 출력해보세요.

2 * 1 = 2

2 * 2 = 4

.

9 * 9 = 81

 

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		for(int i=1; i<10; i++) //단
			for(int j = 1; j<10; j++)
				System.out.printf("%d * %d = %d",i,j,i*j);
				System.out.println();	
}
}

 

 

 

 

✏️for문을 이용하여  구구단 표를 출력해보세요.

구구단 결과 값이 홀수일 때만 출력해보세요.

예를들면, 다음과 같습니다. 3 * 2, 3*  4 의 결과 값이 짝수이기 때문에 생략해요.

3 * 1 = 3

3 * 3 = 9

3 * 5 = 15

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		for(int i=1; i<10; i++) //단
			for(int j = 1; j<10; j++)
				if(i*j%2==1)
				System.out.printf("%d * %d = %d",i,j,i*j);
				System.out.println();	
}
}

 

 

 

 

✏️ 다음 코드를 이용해서, randomNum이 60이상이면 while문을 빠져나가는 코드를 작성해주세요.

while문을 빠져나갈 때 까지 randomNum의 값을 출력해주세요.

int randomNum = (int) (Math.raondom() * 100) + 1

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
	int randomNum = (int) (Math.random() * 100) + 1;
    //랜덤값이 randomNum에 저장됨
	
	System.out.println(randomNum);
    //랜덤값 확인을 위한 출력
    
	while(60<=randomNum) {
    //랜덤넘값이 60이상이면 종료됨 
		System.out.println(randomNum);
        //랜덤값 확인을 위한 출력
		randomNum = (int) (Math.random() * 100) + 1;
        //랜덤넘값이 60이상이 아니면 다시 while문이 돌아가기 위한 구문
	}//while끝
}			
}

 

 

 

 

배열

✏️ 각 기본형 타입별 배열을 모두 선언해보세요. int, byte, boolean 등

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int[] a = new int[3];
		byte [] b = new byte[3];
		boolean [] c = new boolean[3];
	
}	
}

 

✏️ int형 배열 numbers를 선언하세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int[] numbers = new int[3];
        
}	
}

 

 

✏️ int형 배열 numbers는 기본타입인가요?

객체타입이라서 new로 뽑아야 하니까, 기본타입이 아니고 참조타입이다.

 

 

 

✏️ int형 배열 numbers에 크기 10인 배열을 만든 후, 할당하세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int[] numbers = new int[10];
        
}	
}

 

 

 

✏️ int형 배열 numbers의 2번째에 숫자 3을 할당하세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int[] numbers = new int[3];
		numbers[2] = 3;	
        
}	
}

 

 

 

✏️ long형 배열 longNumbers에 크기 3인 배열을 만든 후, 할당하세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		long[] longNumbers = new long[3];
        
}	
}

 

 

 

✏️ long형 배열 longNumbers에 크기 3인 배열을 만든 후, index 2번째를 출력해보세요.

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		long[] longNumbers = {1,2,3};
		System.out.println(longNumbers[2]);
        
}	
}

 

 

 

 

✏️ boolean형 배열 isChecked를 선언하세요. isChecked에 true, false, true를 new연산자없이 할당하세요.

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		boolean[] isChecked = {true,false,true};
        
}	
}

 

 

 

✏️ 크기가 20인 int형 배열 numbers를 선언하세요. numbers의 index 0부터 19까지, for문을 이용해서 0, 1,  2… 19를 할당해주세요

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int[] numbers = new int[20];
		int i=0;
		
		for(i=0; i<20; i++)
			numbers[i]=i;
			System.out.println(numbers[i]);

}	
}

 

 

✏️ 크기가 10인 문자형 배열 chars를 선언하세요. 반복문을 이용하여 chars의 index가 3의 배수일 땐 ‘a’를, 5의 배수일때 ‘b’를 할당하여 출력해보세요.

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		char[] chars = new char[10];
		char i;
		
		for(i=0; i<11; i++)
			if(i%3==0) //3 6 9
				System.out.println('a');
			else if(i%5==0) //5 10
				System.out.println('b');	
		
}	
}

 

 

 

✏️ int형 배열 numbers를 선언하세요. 다음 코드와 형변환을 이용하여 numbers의 각 인덱스에 i+1값을 할당하여 출력해보세요.

for( long i = 0; i < 10; i++) {

}

 

package ex3.control;
public class Homework {
	public static void main(String[] args) {
		
		int[] nembers = new int[10];
	
		for(long i =0; i<10; i++) 
			System.out.println(nembers[(int)i]+(int)(i+1));
	
}	
}

 

 

 

 

✏️Scanner를 통해 다음을 입력받으세요.

20 30 29 39 49 38 10 19 87 29 38 27 8 90 87 -1

크기가 15인 int형 배열에, 입력받은 숫자 중 -1을 제외한 모든 숫자를 차례대로 배열에 할당 후 출력해보세요.

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		
		int[] num = new int[15];
		for(int i=0; i<15; i++) 
			num[i] = sc.nextInt();
			System.out.println(num[i]);

}	
}

 

 

 

✏️ 위의 문제에서, 배열의 5번째 index 값을 출력해보세요.

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		
		int[] num = new int[15];
		
		for(int i=0; i<15; i++) 
			num[i] = sc.nextInt();
		System.out.println(num[5]);
	
}	
}

 

 

✏️ 위의 문제에서, 최댓 값을 찾아 출력하는 코드를 출력해보세요

int max = -1;

package ex3.control;
import java.util.Scanner;
public class Homework {
	public static void main(String[] args) {
		
		int max = -1;
		Scanner sc = new Scanner(System.in);
		int[] num = new int[15];
		
		for(int i=0; i<15; i++) {
			num[i] = sc.nextInt();
			max = num[i];
			
			if(max>=90)
				System.out.println(max);
		}//for끝
}	
}

 

 

 

 

 

뽀나쓰 문제


package ex3.control;
public class Newclass1 {
	public static void main(String[] args) {
		
		//int형 변수 num을 선언한후, 10을 할당해주세요
		//크기가 num인 문자배열,carrays를 작성해주세요
		
		//for문안에 문제들~~~~~~~~~~~~~
		//for문을 이용하여 i가 num번 반복되는 코드를 작성해주세요. 
		//sop에 프린트f를 사용해서 i의값은 i입니다 를 작성해주세요**
		//i가 짝수일때 뉴렉을 출력하는 코드를 작성해주세요**
		//스위치문을 사용하여 i가 3일때, cArrays의 i번째에, 문자 'a'를 넣어주세요**
		
		//i가 5일때 carrays에 i번째에 i+93을 문자로 형변환하여 넣어주세요
		//for문을 이용해 carrays에 잇는 값들을 출력하시오
		
		
		
		int num = 10;
		char[] cArrays = new char[num];
		
		for(int i=0; i<=num; i++) {
			System.out.printf("i의 값은 %d입니다",i);
			System.out.println();
			if(i%2==0)
				System.out.println("뉴렉");
			switch(i) {
				case 3: //i가 3일경우 라는 뜻
					cArrays[i]='a';
                    break;
				case 5:
					cArrays[i] = (char)(i+93);	
                    break;
			}
		}	

		for(int i=0; i<num; i++)
			System.out.println(cArrays[i]);	
	}

}

 

 

 

 

728x90

'국비수업 > CODE' 카테고리의 다른 글

code 실습 / 8️⃣  (0) 2023.02.24
code 실습 / 7️⃣  (0) 2023.02.24
code 실습 / 5️⃣  (0) 2023.02.22
code 실습 / 4️⃣  (0) 2023.02.21
code 실습 / 3️⃣  (0) 2023.02.20