본문 바로가기

PowerJava23

p596)9번 문제 - 심사위원 10명의 점수를 입력 받아라(최고점수, 최저점수 제외) 10명의 점수를 입력받고 점수의 합을 구해라. (단, 최고 점수와 최저 점수는 제외) ArrayList n = new ArrayList(); Scanner sc = new Scanner(System.in); for (int i=0 ; i 2022. 4. 9.
p595) 문제 7번 - 국가를 입력하여 수도를 출력하시오(Map 사용) 사용자로부터 국가를 입력받아 수도를 출력하시오. (국가 이름은 Key로 하고 수도를 value) 결과 값 HashMap map = new HashMap(); map.put("USA", "Washington"); map.put("Japan", "Tokyo"); map.put("China", "Beijing"); map.put("UK", "London"); map.put("Korea", "Seoul"); System.out.print("국가의 이름을 입력하시오>> "); Scanner sc = new Scanner(System.in); String country = sc.next(); System.out.println(country+"의 수도: " +map.get(country)); 2022. 4. 9.
p595) 8번 문제 - 이름이 등장하는 횟수를 Map에 저장해보자 각 이름 성의 개수를 출력해라. names[i] 키를 가져와서, 해당 키 값이 null이라면 1을 넣고 null이 아닐 때 (이미 성씨가 들어가 있을 경우) +1을 해서 카운트하는 개념 String names[] = {new String("kim"), new String("Choi"), new String("Park"), new String("kim"), new String("kim"), new String("Park"), new String("Park")}; HashMap map = new HashMap(); for (int i=0; i 2022. 4. 9.
p370) 3번 문제 - 가위바위보 Random을 이용하여 0,1,2를 가위,바위,보 맵핑하고 아래와 같이 출력하면 됨 하나를 선택하십시오: 가위(0), 바위(1), 보(2) : 1 컴퓨터는 바위를 냈습니다. 비겼습니다. Random r = new Random(); int com_n = r.nextInt(3); //랜덤하게 0,1,2 값 저장 (컴퓨터가 낸 가위, 바위, 보 중 하나) String str[] = {"가위", "바위", "보"}; // 가위바위보를 0,1,2와 맵핑하기 위해 String str1=""; // 가위바위보를 맵핑 후 컴퓨터의 값이 저장되는 변수 Scanner sc = new Scanner(System.in); System.out.println("가위(0), 바위(1), 보(2): "); int n = sc.nex.. 2022. 4. 7.
p370) 1번 문제 - 문자열을 입력 받아 아래와 같이 출력하시오 문자열의 split()메소드를 이용하여서 문자열을 단어로 분리한다. 공백을 기준으로 split 하고 카운트를 매긴다 문자열을 입력하시오: This is a house This, is, a, house 모두 4개의 단어가 있습니다. Scanner sc = new Scanner(System.in); System.out.println("문자열을 입력하시오:"); String input = sc.nextLine(); String []spl = input.split(" "); int i=0; for (i=0; i 2022. 4. 7.
p216)MiniProject - 책 정보 저장 책 정보는 저장 및 검색하는 프로그램 작성 아래와 그림과 같은 내용을 출력하게 끔 만들어보자 1. Book 클래스를 생성하여 작업 class Book { String title;//도서명을 위한 맴버변수 int score;//평점을 위한 맴버변수 static int count;//모든 도서의 수를 저장하기 위한 클래스 변수 public Book(String title, int score) {//생성자 super(); this.title = title; this.score = score; count++; } public Book() { super(); // TODO Auto-generated constructor stub count++; } @Override public String toString() {.. 2022. 4. 6.
p182) 주사위 게임 주사위의 합이 2가 되면 탈출하고 2가 나오는데 걸린 횟수를 출력해라 --- 출력 값 --- a1> 1 a2> 1 count 5 class Dice class Dice{ private int value; public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Dice() { value=0; } } Main Dice dice = new Dice(); Random r = new Random(); int n = 0; int cnt=1; int a1=0; int a2=0; while(true) { n = r.nextInt(6)+1; dice.setValue(n); a1=dice.get.. 2022. 4. 4.
p123 ) MiniProject - 숫자 추측 게임(정수 맞추기) 사용자가 정수 하나를 입력하여 프로그램이 가지고 있는 정수를 맞추기 입출력 값 int collectNumber = 2; // 맞춰야 할 정수 Scanner sc = new Scanner(System.in); int inputNumber=0; int loopCount =0; while(true) { System.out.print("정답을 추측하여 보시오: "); inputNumber = sc.nextInt(); if (loopCount==7) { System.out.println("기회 초과입니다. 틀렸습니다"); break; } if(collectNumber == inputNumber) { System.out.println("축하합니다"); System.out.println("inputNumber: "+.. 2022. 4. 3.
p130) 12번 문제풀이 - 성적 입력 후 합계 및 평균 구하기(ArrayList사용) ArrayList ArrayList ArrayTest= new ArrayList(); ArrayTest.add(입력 값); //동적으로 배열을 계속 넣을 수 있음 ArrayTest.get( i ) // 넣었던 입력 값을 배열의 값을 가져옴 ArrayTest.size( ) // 배열의 크기 반환 성적을 입력 받아 합계, 평균을 출력하시오 성적을 입력 받아 합계, 평균을 출력하시오 -1이 나오면 성적 입력 stop 입력 값 & 출력 값 Scanner sc = new Scanner(System.in); ArrayList n = new ArrayList(); int data; while(true){ System.out.println("성적을 입력하세요"); data = sc.nextInt(); if (data .. 2022. 4. 1.
p129) 10번문제 - 배열의 합/최대값 구하기 해당 배열의 최대값과 앞을 구해라 주의 : max 초기화 할때 배열의 첫번째 값으로 할 것( 0으로 하면 음수 값을 출력 못함 ) --- 출력문 --- -5.0 -1.0 -2.0 -3.0 -4.0 최대 값 -1.0 합은 -15.0 double[] d = {-5.0, -1.0, -2.0, -3.0, -4}; int i=0; int j=0; double sum=0; double max=d[0]; for (i=0; i 2022. 3. 31.
p130) 13번 문제, 카드 선택 (Random 함수) 랜덤(Random)함수 사용 아래 코드의 name[], num[]을 랜덤하게 출력해라 --- 예시 출력 --- Diamonds 의 2 Hearts 의 5 Clubs 의 Ace Spades 의 6 Hearts 의 King String name[]= {"Clubs", "Diamonds", "Hearts", "Spades"}; String num[]= {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; Random r = new Random(); int i=0; while(true) { String nameRandom =name[r.nextInt(name.length)]; String nameNum =num[r.next.. 2022. 3. 31.
p129) 9번, 피보나치 수열 피보나치 수열을 구하시오 --- 입력 값 --- 10 --- 출력 값 --- 0 1 1 2 3 5 8 13 21 10개의 값이 나오게 끔 int n=10; int before_sum=1; int sum=0; System.out.print("0 "); for(int i=0; i 2022. 3. 30.
p128)8번 피타고라스 문제 a제곱 + b제곱 = c제곱 (각변의 길이는 100보다 작음) 을 만족하는 각 변의 길이를 구해라. 3중 for문을 이용. --- 출력문 --- 3 4 5 4 3 5 5 12 13 int a,b,c; for(a=1 ; a 2022. 3. 30.
p128)6번 문제, 1차 방정식을 만족하는 해를 구하기 3x+10y=100을 만족하는 해를 구해라 0 2022. 3. 30.
p128)5번 문제, 두 개의 주사위 두 개의 주사위를 던져 6이 나오는 경우의 수를 구해라 (1 5) , (2 4) , (3 3) , (4 2) , (5 1) , int cnt=0; int i,j; for(i=1 ; i 2022. 3. 30.
반응형