Java

JAVA 자바 웹 개발 주말 연습 문제 풀기 02/12

jnk1m 2022. 2. 13. 02:52

35.

package day04;

import java.util.Scanner;

public class Test35_1 {

	public static void main(String[] args) {
	// 문제2-1. 게임 랭킹 보드. 5개의 데이터를 저장할 수 있는 users와 scores라는  
 // 배열이있고, users배열에는 유저네임, scores 배열에는 유저의 게임점수를 입력받는다. 
	 // 단, 유저네임과 게임점수는 배열 인덱스상으로 서로 일치해야한다. 
		Scanner sc = new Scanner(System.in);
		
		String [] users = new String [5];
		int [] scores = new int [5];
		
		for(int i = 0; i < users.length; i++) {
			System.out.println("유저 네임>> ");
			String name = sc.nextLine();
			users[i]= name;
			System.out.println("점수>> ");
			int num = Integer.parseInt(sc.nextLine());
			scores[i]=num ;
			System.out.println("\n");
		}
		System.out.println("*2번으로\n");
		

	      // 문제2-2. 위 저장된 users와 scores를 아래와 같은 형태로 출력해보세요. 
	     //       출력예)
	     //          user_name      score
	      //         ----------------------
	      //         피카츄          87
	      //         파이리          24
	      //         ....

		System.out.println("user_name\tscore");
		System.out.println("-----------------------");
		for(int i = 0; i < users.length; i++) {
			System.out.print(users[i] + "\t\t" + scores[i]+ "\n");
		}
		System.out.println("\n*3번으로\n");
		
	      // 문제2-3. 두번째 유저의 점수와 세번째 유저의 점수가 바뀌어 저장되었다 하네요.
	      //         교환해서 저장해주세요. 
		int tmp =scores[1];
		scores[1]=scores[2];
		scores[2]=tmp;
		
		System.out.println("user_name\tscore");
		System.out.println("-----------------------");
		for(int i = 0; i < users.length; i++) {
			System.out.print(users[i] + "\t\t" + scores[i]+ "\n");
		}
		System.out.println("\n*4번으로\n");		
		
		
	               
	      // 문제2-4. 점수가 높은순으로 게임 랭킹보드를 만들고, 출력해주세요. (1~5위) 
	            /*출력예)
	                rank      user_name      score
	                ---------------------------------
	                1         꼬북이         97
	                2         피카츄         87
	                3...
	    }*/
		
		for(int i =0; i <users.length;i++) {
			for(int j = i +1; j < users.length; j++) {
				if(scores[i] < scores[j]) {
					int tmp2 = scores[i];
					scores[i]=scores[j];
					scores[j]=tmp2;
					String tmp3 = users[i];
					users[i]=users[j];
					users[j]=tmp3;
					
				}
			}
		}
		System.out.println("rank\tuser_name\tscore");
		System.out.println("-------------------------------");
		for(int i = 0; i < users.length; i++) {
			System.out.print((i+1) + "\t"+ users[i] + "\t\t" + scores[i]+ "\n");
		}
		System.out.println("\n*5번으로\n");	
	      
	      // 문제2-5. 또 한명의 유저가 게임을 끝냈습니다. 
	      //         점수와 유저네임을 입력받고, 기존의 게임 랭킹보드를 업데이트 해주세요. 
	      //         이때 입력받은 새로운 유저의 데이터는 기존데이터에 추가.(1-3문제참고) 
	      //         저장 후, 랭킹을 출력하시되 1~5위까지만 출력해주세요. 
		
		System.out.println("새로운 유저 네임>>");
		String new_name= sc.nextLine();
		System.out.println("점수>>");
		int new_num = Integer.parseInt(sc.nextLine());
		System.out.println();
		
		String [] new_users = users;
		users = new String [6];
		int [] new_scores = scores;
		scores = new int [6];
		
		int len = scores.length;
		for(int i = 0; i < new_users.length;i++) {
			users[i]=new_users[i];
			scores[i]=new_scores[i];
		}users[len-1]=new_name;
		scores[len-1]=new_num;
		
		for(int i =0; i <users.length;i++) {
			for(int j = i +1; j < users.length; j++) {
				if(scores[i] < scores[j]) {
					int tmp2 = scores[i];
					scores[i]=scores[j];
					scores[j]=tmp2;
					String tmp3 = users[i];
					users[i]=users[j];
					users[j]=tmp3;
					
				}
			}
		}
		System.out.println("rank" + "\t" + "user_name"+"\t"+ "score");
		System.out.println("-------------------------------");
		for(int i = 0; i < 5; i++) {
			System.out.print((i+1) + "\t"+users[i] + "\t\t" + scores[i]+ "\n");
		}
		System.out.println("\n종료!\n");	
		
	}

}

 

실행 결과