<문제>

로또 6개 1~45 사이 번호 생성

중복 x

public abstract class test {
	

	public static void main(String[] args) {

		int[] lotto = new int[6];
		
		// 번호 생성
		for(int i =0; i<6; i++) {
			
			lotto[i] = (int)(Math.random()*45)+1;		
			// Math.random 0.0 ~1.0 미만의 난수
			// 로또번호 1~45 이므로 *45 하여 0.0 ~ 45.0
			// 후 +1 하면 1 이상 46 미만의 난수 생성
			
			//중복값 제거
			for(int j=0; j<i; j++) {
				if(lotto[i] ==lotto[j]) {
					i--;
					break;
				}
			}
			
			// 중복될 경우 i값을 -1 감소해줬으므로 다시 난수를 뽑을 때
			// 다시 i 번째 값을 뽑으므로 중복됐던 값 위에 새로운 값이 덮어씌어진다
		}
		
		System.out.print("로또 번호 : ");
		
		// 번호 풀력
		for(int i =0; i<6; i++) {
			System.out.print(lotto[i] + "/ ");
		}
	      
	}
}

 

<결과>

<문제>

1. 중복되는 숫자 개수 구하기

	public static void main(String[] args) {
		
		int A = 22;
		int B = 456;
		int C = 44;
		
		int[] counts = new int[10];	// 나머지가 0~9 이므로 총 10개의 인덱스 필요 
		
		int number = A*B*C;
		System.out.println("세 정수 곱 : " + number);
	
		while(number>0) {
			counts[number%10]++;
			// 인덱스는 0부터 시작하므로
			// 나머지 값이 1일 경우 두번째 인덱스 값 + 1
			// 즉, 나머지 3이 두 번 나오면 4번째 인덱스의 값 2가 됨 
			number /= 10;
		}
		
		for(int i = 0; i<counts.length; i++) {
			System.out.println(i + " 개수 : " + counts[i]);
		}
	}

<실행 결과>

<문제>

2. 주어진 이름 배열중에서 중복되는 성 개수를 구하고, 어떤 성이 제일 많은지 찾아라

Collections.frequency 사용

 

	public static void main(String[] args) {
		
		List<String> list = new ArrayList<>(Arrays.asList("이하나", "이둘", "이셋", "김넷", "박다섯"));

		System.out.println("원본  : " + list);
		
		ArrayList<Character> list2 = new ArrayList<>();

		for(int i = 0; i<list.size(); i++) {
			list2.add(list.get(i).charAt(0));
		}
		
		//ArrayList<Character> set = new ArrayList<Character>(list2);
		// 중복이 허용되므로 중복허용하지 않는 HashSet으로 변경해줘야함
		/*
		 * 이씨 몇 명 ? 3
			이씨 몇 명 ? 3
			이씨 몇 명 ? 3
			김씨 몇 명 ? 2
			박씨 몇 명 ? 1
			김씨 몇 명 ? 2
		 */
		
		Set<Character> set = new HashSet<Character>(list2);
		// ArrayList를 중복이 없는 Collection 객체인 HashSet으로 변경
		/*
		 * 김씨 몇 명 ? 2
		       이씨 몇 명 ? 3
		       박씨 몇 명 ? 1
		 */
		
		for(Character str : set) {
			System.out.println(str + "씨 몇 명 ? " + Collections.frequency(list2, str));
		}
		
		System.out.println("가장 많은 성씨는 ? " + Collections.max(list2));
 
	}

<실행결과>

 

 

 

 

 

<문제>

문자열 s는 한 개 이상의 단어로 구성되어 있습니다. 각 단어는 하나 이상의 공백문자로 구분되어 있습니다.

각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을 리턴하는 함수, solution을 완성하세요.

 

String s = "try hello world";
		
		String answer ="";
		int idx = 0;
		
		String[] arr = s.split("");
		
		for(int i =0; i <arr.length; i++) {
			
			if((" ").equals(arr[i])){
				idx=0;
			}else {
				
				if(idx%2 == 0) {
					idx++;
					arr[i] = arr[i].toUpperCase();
				}
				else {
					idx++;
					arr[i] = arr[i].toLowerCase();
				}
				
			}
			answer += arr[i];	
		}
		
		System.out.println(answer);

 

<실행 결과>

<문제>

switch 구문을 하용하여 세 과목을 입력 받은 후,

평균 90 이상 A, 80 이상 B, 70 이상 C, 나머지 F

(단, 한 과목이라도 50점 이하시, 무조건 F)

 

 

public class test {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		
		System.out.print("수학 성적 : ");
		int math = sc.nextInt();
		
		System.out.print("과학 성적 : ");
		int sci = sc.nextInt();
		
		System.out.print("영어 성적 : ");
		int eng = sc.nextInt();
		
		int tot = math + sci + eng;
		int avg = tot/3;

		char grade = 'F';
		
		if(math < 50 || sci < 50 || eng < 50) {	// 한 과목이라도 50점 이하시 F

		}else {
			
			switch((int)avg/10) {
			case 10:
			case 9 : {grade = 'A'; break;}  // 90~99 까지
			case 8 : {grade = 'B'; break;}
			case 7 : {grade = 'C'; break;}	
			default : {grade = 'F';}
			}
			
		}
		
		System.out.println("평균 : " + avg);
		System.out.println("학점 : " + grade);

	}

}

 

<실행 결과>

+ Recent posts