본문 바로가기
IT/프로그래밍

명품 JAVA Programming 3장 실습문제 정답

by nutrient 2020. 12. 12.
728x90
728x170

명품 JAVA Programming 3장 실습문제 정답

 

명품 JAVA Programming 3장 실습문제 정답

 

1번

(1) 2450

(2)

public class WhileTest {
 public static void main(String[] args) {
 int sum=0, i=0;
 while (i<100)
 {
 sum = sum + i;
 i +=2;
 }
 System.out.println(sum);
 }
 }

(3)

public class ForTest {
 public static void main(String[] args){
 int sum=0;
 for(int i=1;i<100;i++)
 {
 sum += sum + i;
 i += 2;

 }
 System.out.println(sum);
 }
 }

 

2번

public class Print2DArray {

 public static void main(String[] args) {
  int n [][] = { {1}, {1,2,3}, {1}, {1,2,3,4}, {1,2}};
  
  for(int i=0; i<n.length; i++) {
   for(int j=0; j<n[i].length; j++) {
    System.out.print(n[i][j] + "\t");
   }
   System.out.println();   
  }

 }

}

3번

import java.util.Scanner;

public class PrintAsterisk {
 public static void main(String[] args){
  Scanner scanner = new Scanner(System.in);
  System.out.print("정수를 입력하시오>>");
  int n = scanner.nextInt();
  if (n <=0) {
   System.out.println("0보다 커야 합니다.");
   scanner.close();
   return;
  }
  
  for (int i=n; i>0; i--) { // n줄 출력
   for (int j=0; j<i; j++) { // i개의 별표를 한 줄에 출력
    System.out.print('*');
   }
   System.out.println(); // 다음 줄로 넘어가기
  }
  scanner.close();
 }
}

순자산 8.4억이면 상위 10%.JPG

 

순자산 8.4억이면 상위 10%.JPG

인터넷에서 유명한 글인 순자산 8.4억이면 상위 10%에 대해 알아보도록 하겠습니다. 이 글을 처음부터 끝까지 읽다 보면 순자산 8.4억이면 상위 10%에 대해 아는데 도움이 될 것입니다. 순자산 8.4

tistorysolution.tistory.com

4번

import java.util.Scanner;

public class PrintAlphabet {
 public static void main(String[] args){
  Scanner scanner = new Scanner(System.in);
  System.out.print("소문자 알파벳 하나를 입력하시오>>");
  String s = scanner.next();
  if(s.length() != 1) {
   System.out.print("알파벳 하나만 입력해야 합니다!");
   scanner.close();
   return;
  }
  
  char c = s.charAt(0);
  if (c < 'a' || c > 'z') {
   System.out.println("소문자 알파벳이 아닙니다.");
   scanner.close();
   return;
  }
  
  for (char i=c; i>='a'; i--) {
   for (char j='a'; j<=i; j++)
    System.out.print(j);
   System.out.println();
  }
  
  scanner.close();
 }
}

5번

import java.util.Scanner;

public class MutipleOfThree {
 public static void main (String[] args) {
  int intArray[] = new int[10];
  Scanner scanner = new Scanner(System.in);
  
  System.out.print("양의 정수 10개를 입력하시오>>");
  for (int i=0; i<intArray.length; i++) 
   intArray[i] = scanner.nextInt();
  
  System.out.print("3의 배수는 ");
  for (int i=0; i<intArray.length; i++)
   if (intArray[i] % 3 == 0) // 3으로 나누어 나머지가 0이면 3의 배수
    System.out.print(intArray[i] + " ");
  
  scanner.close();
 }
}

6번

import java.util.Scanner;

public class ChangeMoney {
 public static void main (String args[])  {
  int [] unit = {50000, 10000, 1000, 500, 100, 50, 10, 1}; // 환산할 돈의 종류

  Scanner scanner = new Scanner(System.in);

  System.out.print("금액을 입력하시오>>");
  int money = scanner.nextInt();
  
  for(int i=0; i<unit.length; i++) {
   int res = money/unit[i]; // unit[i]의 개수 계산
   
   // res는몫
   if (res > 0) { // 몫이 있는 경우
    System.out.println(unit[i] + "원 짜리 : " + res + "개");
    money = money%unit[i]; // money 갱신
   }
  }
  
  scanner.close();
 }
}

7번



public class RandomTen {

 public static void main(String[] args) {
  int n [] = new int [10]; // 배열 생성
  
  for(int i=0; i<n.length; i++) { // 10개의 랜덤한 정수 생성 및 저장
   int r = (int)(Math.random()*10 + 1);
   n[i] = r;
  }
  
  int sum = 0;
  for(int i=0; i<n.length; i++) // 합 구하기
   sum += n[i];
  
  System.out.print("랜덤한 정수들 : ");
  for(int i=0; i<n.length; i++)
   System.out.print(n[i] +" ");
  
  System.out.println("\n평균은 " + (double)sum/n.length);
 }

}

 

8번


import java.util.Scanner;

public class RandomArray {
 public static boolean exists(int a[], int from, int r) {
  for(int j=0; j<from; j++) {
   if(a[j] == r) {
    return true; // exists 
   }
  }  
  return false; // not exists
 }


 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.print("정수 몇개?");
  int n = scanner.nextInt();
  
  if(n <= 0 || n > 100) {
   System.out.print("1~100사이로 입력하세요!");   
   scanner.close();
   return; // 프로그램 종료
  }
  int array [] = new int [n]; // n개의 정수 배열 생성
  
  for(int i=0; i<array.length; i++) {
   int r = (int)(Math.random()*100 + 1); // 1~100 범위의 랜덤 정수
   // 정수 r의 이미 배열 array[0]~array[i-1]에 있는지 검사
   if(exists(array, i, r)) {
    i--; // not to increase index i in for.
    continue;
   }
   array[i] = r;   
  }
  
  for(int i=0; i<array.length; i++) {
   if(i%10 == 0) System.out.println(); // 다음 줄로 넘어가기
   System.out.print(array[i] + " ");
  }
  
  scanner.close();
 }
}

9번


public class twoDArray {
 public static void main (String[] args) {
  int intArray[][] = new int[4][4];
  
  for (int i=0; i<intArray.length; i++)
   for (int j=0; j<intArray[i].length; j++)
    intArray[i][j] = (int)(Math.random()*10 + 1); // 1부터 10사이의 임의의 수 생성하여 저장
  
  for (int i=0; i<intArray.length; i++) {
   for (int j=0; j<intArray[i].length; j++)
    System.out.print(intArray[i][j] + "\t");
   System.out.println();
  }
 }
}

 

10번


public class twoDArray {
 public static void main (String[] args) {
  int intArray[][] = new int[4][4];

  // 배열의 모든 원소를 0으로 초기화 
  for (int i=0; i<intArray.length; i++)
   for (int j=0; j<intArray[i].length; j++)
    intArray[i][j] = 0;

  int num = 0;
  while (num < 10) { // 10개만 랜덤한 정수 생성
   int row = (int)(Math.random()*4); // 배열의 행 인덱스 생성
   int col = (int)(Math.random()*4); // 배열의 열 인덱스 생성
   
   if (intArray[row][col] != 0) // 배열 원소가 0이 아니면 이미 값이 저장된 원소이므로 건너뜀
    continue;
   else { 
    intArray[row][col] = (int) Math.round(Math.random()*9 + 1);
    num++; // 생성된 숫자 갯수 증가
   }
  }
  
  // 2차원 배열 출력
  for (int i=0; i<intArray.length; i++) {
   for (int j=0; j<intArray[i].length; j++)
    System.out.print(intArray[i][j] + "\t");
   System.out.println();
  }
 }
}

11번


public class Average {
 public static void main (String[] args) {
  if(args.length == 0) {
   System.out.println("명령행 인자가 업습니다.");
   return;
  }
  int sum=0;
  for (int i=0; i<args.length; i++) {
   sum = sum + Integer.parseInt(args[i]); // 인자를 정수로 변환하여 합산
  }
  System.out.println(sum/args.length); // 평균 출력
 }
}

이젠 무슨 짓을 해도 집값 못올라간다

 

이젠 무슨 짓을 해도 집값 못올라간다

인터넷에서 유명한 글인 이젠 무슨 짓을 해도 집값 못올라간다에 대해 알아보도록 하겠습니다. 이 글을 처음부터 끝까지 읽다 보면 이젠 무슨 짓을 해도 집값 못올라간다에 대해 아는데 도움이

tistorysolution.tistory.com

12번

public class Add {
 public static void main (String[] args) {
  int sum=0;
  for (int i=0; i<args.length; i++) {
   try {
    int n = Integer.parseInt(args[i]); // 인자를 정수로 변환
    sum = sum + n;
   } catch(NumberFormatException e) {
    // 정수로 변환할 수 없는 인자는 NumberFormatException 예외가 발생하며 합산에서 제외
   } 
  } 
  System.out.println(sum); 
 }
}

 

13번

public class ThreeSixNine {
 public static void main (String args[])  {
  String str[] = {" 박수 짝", " 박수 짝짝"};
  int res, num, numberOf369 = 0;

  for (int i=1; i<100; i++) {
   num = i;
   for (res = num % 10; num > 0; res = num % 10) {
    if (res == 3 || res == 6 || res == 9) numberOf369++; // 정수에 3,6,9중 하나가 있는 경우  numberOf369 증가
    num = num / 10;
   }
   if (numberOf369 >0) // 수에 3,6,9가 하나 이상 있는 경우
    System.out.println(i + str[numberOf369-1]); 
   numberOf369 = 0;
  }
 }
}

14번


import java.util.Scanner;

public class ScoreArray {

 public static void main(String[] args) {
  String course [] = { "Java", "C++", "HTML5", "컴퓨터구조", "안드로이드" };
  int score [] = {95, 88, 76, 62, 55};

  Scanner scanner = new Scanner(System.in);
  
  while(true) {
   System.out.print("과목 이름>>");
   String name = scanner.next();
   if(name.equals("그만")) 
    break;

   int i;
   for(i=0; i<score.length; i++) {
    if(course[i].equals(name)) {
     System.out.println(name + "의 점수는 " +  score[i]);
     break;
    }
   }
   
   if(i==score.length) // 과목명이 잘못된 경우
    System.out.println("없는 과목입니다.");
  }
  
  scanner.close();
 }

}

15번


import java.util.InputMismatchException;
import java.util.Scanner;

public class Multiply {
 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  do {
   System.out.print("곱하고자 하는 두 수 입력>>");
   try {
    int n = scanner.nextInt();
    int m = scanner.nextInt();
    System.out.print(n + "x" + m + "=" + n*m);
    break;
   } catch(InputMismatchException e) {
    System.out.println("실수는 입력하면 안됩니다.");
    scanner.nextLine(); // <Enter> 키까지 읽어서 버린다.
    continue;
   }
  } while(true);

  scanner.close();
 }
}

스포츠카를 못타는 인생은 진짜 불쌍한 인생이다

 

스포츠카를 못타는 인생은 진짜 불쌍한 인생이다

인터넷에서 유명한 글인 스포츠카를 못타는 인생은 진짜 불쌍한 인생이다에 대해 알아보도록 하겠습니다. 이 글을 처음부터 끝까지 읽다 보면 스포츠카를 못타는 인생은 진짜 불쌍한 인생이다

tistorysolution.tistory.com

 

728x90
그리드형

댓글