상세 컨텐츠

본문 제목

[java] 배열 연습문제 #2

java

by ~지우~ 2021. 7. 6. 10:55

본문

728x90

오늘은 「java_an_introduction_to_problem_solving_and_programming_6th_edition」 564페이지 9번 문제를 풀어보겠습니다.



<문제>

출처: java_an_introduction_to_problem_solving_and_programming_6th_edition  

 

비밀번호 입력 시 각 숫자에 임의의 숫자를 부여하여 비밀번호를 입력하도록 한 후, 미리 정해둔 비밀번호와 일치할 경우 "correct" 를 출력하고 틀릴 경우 "wrong" 을 출력하는 코드를 작성해야 합니다.

 

import java.util.Scanner;
public class Password {

	public static void main(String[] args) {
		int[] pin = new int[10];
		int num;
		int []realPin = {0,2,7,4,5};
		int j = 0, index = 0;
		int [] inputPin = new int[5];
		for(index=0;index<10;index++) {
			num = (int)(3.0*Math.random()+1);
			pin[index] = num;
			System.out.print("PIN: " + index + " ");
		}
		System.out.println();
		for(int i=0;i<10;i++) {
			System.out.print("NUM: "+ pin[i] + " ");
		}
		System.out.println();
		System.out.println("enter pin: ");
		for(int i=0; i<5;i++) {
			Scanner keyboard = new Scanner(System.in);
			inputPin[i] = keyboard.nextInt();
		}
		for(int i=0;i<5;i++) {
			if(inputPin[i] == pin[realPin[i]]) {
				if(i==4)
					System.out.println("correct");
			}	
			else if(inputPin[i] != pin[realPin[i]]) {
				if(i==4)
					System.out.println("wrong");
			}
					
		}
		
	}	

}
728x90

관련글 더보기

댓글 영역