본문 바로가기
스마트인재개발원/자바

[자바]배열을 이용하여 채점 프로그램/배열의 수만큼 별 찍기/홀수만 출력

by 죠졍니 2022. 9. 15.
728x90
반응형
SMALL

 

사용자로부터 답을 입력받아 결과 출력 총점 출력 배점은 20

 

 

1 4 3 2 1

==채점하기==

답을 입력하세요

1번답 >>

2번답 >>

3번답 >>

4번답 >>

5번답 >>

정답확인

x x x o x 총점:20

 

 

 

import java.util.Scanner;

public class Ex03_배열실습2 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		int[] answer = new int[5];
		
		answer[0]=1;
		answer[1]=4;
		answer[2]=3;
		answer[3]=2;
		answer[4]=1;
		
		System.out.println("==채점하기==");
		System.out.println("답을 입력하세요");
		
		int[] user_answer = new int[5];
		for(int i=0;i<user_answer.length;i++) {
			System.out.print(i+1 +"번답 >> ");
			user_answer[i]=sc.nextInt();
		}
		
		System.out.println("정답확인");
		int sum=0;
		for(int i=0;i<user_answer.length;i++) {
			if(answer[i]==user_answer[i]) {
				System.out.print("o ");
				sum+=20;
			}
			else {
				System.out.print("x ");
			}
		}
		System.out.println("총 점 : "+sum);	
	}

}​

 

 

 

 


문제3. 배열의 들어있는 수 만큼 별 출력

int[] starcount = {3,4,4,2,1};

 

3: ***

4: ****

4: ****

2: **

1:

 

 

 

 

public class Ex04_배열실습3 {

	public static void main(String[] args) {

		int[] starcount = {3,4,4,2,1};
		
		for(int i=0;i<starcount.length;i++) {
			System.out.print(starcount[i]+": ");
			
			for(int j=0;j<starcount[i];j++) {
				
				System.out.print("*");
				
			}
			System.out.println();
			
		}		
	}

}

 

 

 

 


 

 

foreach실습2

1차원 정수형 배열 선언후 임의의 값으로 초기화

배열의 값 중 홀수인 값이 몇 개인지 출력하는 프로그램

 

 

 

public class Ex06_foreach실습2 {

	public static void main(String[] args) {

		int[] array = {3, 7, 71, 9, 10};
		
		System.out.print("총 ");
		 
		int cnt=0;
	        for(int count:array){
	            if(count%2==1) 
	            cnt+=1;
	        }
		System.out.print( cnt+ "개 입니다.");
		
	}

}

 

 

 

 

 

 

 

728x90
반응형
LIST