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
'스마트인재개발원 > 자바' 카테고리의 다른 글
[자바] 치환/ 버블정렬 (0) | 2022.09.16 |
---|---|
[자바]이차원배열 초기화 및 출력 다양한 형태 (0) | 2022.09.16 |
[자바]배열 5개 입력받아 모두 출력 후 최대,최소,총합,평균 구하기 (0) | 2022.09.15 |
[자바] 배열 (0) | 2022.09.14 |
[자바] 이중for문 이용하여 별찍기 예제들 (0) | 2022.09.14 |