[Power Java Compact] 3장 연습문제

2021. 12. 15. 17:26·Major/Java
728x90

* 혼자 푼거라 오답이 있을 수 있습니다. 오답이 있으면 댓글로 남겨주세요

 


Chapter 3 선택과 반복

 

Mini project -p91

import java.util.Scanner;
public class game {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int answer = (int) (Math.random()*100);
		int guess, cnt=0;
		do {
			System.out.print("정답을 추측하여 보시오: ");
			guess = sc.nextInt();
			if(guess<answer) {
				System.out.print("LOW\n");
				cnt++;
			}
			if(guess>answer){
				System.out.print("HIGH\n");
				cnt++;
			}
		}while(guess !=answer);
		System.out.printf("축하합니다. 시도횟수=%d", cnt);
	}

}

 

Mini Project2 -p104

public class card {
	public static void main(String[] args) {
		String[] shape = { "Clubs", "Diamonds", "Hearts", "Spades" };
		String[] num = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
		
		for(int i=0;i<5;i++) {
			int ran1 = ((int)(Math.random()*10))%4;
			int ran2 = ((int)(Math.random()*100))%13;
			System.out.printf("%s의 %s\n", shape[ran1], num[ran2]);
		}
	}
}

Exercise

1. 

1) if(count>=20 && count<60) count++;

2) if(x>y) max=x; if(x<y) max=y;

3) if(x>0 && x<=20) y=x;

 

2. 

one two done

 

3.

아무것도 출력안됨

else부분을 한번 들여쓰기한다.

 

4.

컴파일 에러

i는 for문안에서 생성된 지역변수이므로 for문 밖에서 출력할 수 없다.

 

5.

1)

age가 0~18인걸 하려면

age>0 && age<18 로 하거나 이중 for문으로 표기해야함

0<age<18 로 하면 오류남

 

2) 

부등호를 1개가아니라 2개를 써야함 x==0

x=0은 x에 0을 대입하겠다는 소리임

 

6.

0=0

3=3

6=6

9=9

 

7.

for(int i=1;i<30;i++){
	System.out.println(i*i+1);
}

8.

①②④

 

9.

1)
int[] studentNumbers = new int[30];
2)
double[] values = { 1.2, 3.3, 6.7 };

10.

1) 0~4

2) 에러남

 

11.

for(int i=0;i<100;i++){
	values[i] = 0;
}

 

 

 


Programming Exercise

1.

public class exercise {
	public static void main(String[] args) {
		int ans = 0;
		for(int i=1;i<=100;i++) {
			if(i%3 == 0) ans+=i;
			if(i%4 == 0) ans+=i;
			if(i%3==0 && i%4 == 0)	ans-=i;
		System.out.print(ans);
	}
}

 

2.

import java.util.*;
public class exercise {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("정수를 입력하세요: ");
		int num = sc.nextInt();
		switch (num) {
			case 1:
				System.out.println("ONE");
				break;
			case 2:
				System.out.println("TWO");
				break;
			case 3:
				System.out.println("THREE");
				break;
			case 4:
				System.out.println("FOUR");
				break;
			case 5:
				System.out.println("FIVE");
				break;
			case 6:
				System.out.println("SIX");
				break;
			case 7:
				System.out.println("SEVEN");
				break;
			case 8:
				System.out.println("EIGHT");
				break;
			case 9:
				System.out.println("NINE");
				break;
			default:
				System.out.println("OTHER");			
		}
    }
}

 

3.

public class exercise {
	public static void main(String[] args) {
		int[] dice1 = {1,2,3,4,5,6};
		int[] dice2 = {1,2,3,4,5,6};
		for(int i=0;i<6;i++) {
			for(int j=0;j<6;j++) {
				if(dice1[i]+dice2[j] == 6) {
					System.out.printf("(%d,%d), ",dice1[i], dice2[j]);
				}
			}
		}
    }
}

 

4.

public class exercise {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a, b;
		System.out.print("키를 입력하세요: ");
		a = sc.nextInt();
		System.out.print("몸무게를 입력하세요: ");
		b = sc.nextInt();
		double c = (a-100)*0.9;
		if(c<a) System.out.println("과체중 입니다.");
		else if(c>a) System.out.println("저체중 입니다.");
		else System.out.println("표준체중 입니다.");
    }
}

 

5.

public class exercise {
	public static void main(String[] args) {
		int[] x = {0,1,2,3,4,5,6,7,8,9,10};
		int[] y = {0,1,2,3,4,5,6,7,8,9,10};
		for(int i=0;i<11;i++) {
			for(int j=0;j<11;j++) {
				if(3*x[i]+10*y[j] == 100) System.out.printf("(%d,%d)\n",x[i],y[j]);
			}
		}
    }
}

 

6.

public class exercise {
	public static void main(String[] args) {
		System.out.print("2부터 100사이 모든 소수 : ");
		
		boolean not = false;
		for(int i=2;i<=100;i++) {
			for(int j=2;j<i;j++) {
				if(i%j==0) {
					not = true;
					break;
				}	
			}
			if(not == false) System.out.print(i+" ");
			not = false;
		}
    }
}

 

7.

public class exercise {
	public static void main(String[] args) {
		for(int i=1;i<100;i++) {
			for(int j=1;j<100;j++) {
				for(int k=1;k<100;k++) {
					if(i*i+j*j == k*k) System.out.printf("%d %d %d\n", i, j, k);
				}
			}
		}
    }
}

 

8.

import java.util.*;
public class exercise {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("연산을 입력하세요: ");
		String op = sc.nextLine();
		System.out.print("숫자 2개를 입력하세요: ");
		int x = sc.nextInt();
		int y =sc.nextInt();
		if(op.equals("+")) {
			System.out.printf("%d%s%d = %d",x,op,y,x+y);
		}else if(op.equals("-")) {
			System.out.printf("%d%s%d = %d",x,op,y,x-y);
		}else if(op.equals("*")) {
			System.out.printf("%d%s%d = %d",x,op,y,x*y);
		}else if(op.equals("/")) {
			if(y==0) System.out.println("분모가 0입니다.");
			else System.out.printf("%d%s%d = %d",x,op,y,x-y);
		}
    }
}

 

9.

public class exercise {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("출력할 항의 개수: ");
		int x = sc.nextInt();
		int num1 = 0;
		int num2 = 1;
		for(int i=1;i<=x;i++) {
			if(i==1) System.out.print(num1+ " ");
			else if(i==2)  System.out.print(num2+ " ");
			else {
				int tmp = num2;
				num2 = num1+num2;
				num1 = tmp;
				System.out.print(num2+ " ");
			}
		}
    }
}

 

10.

public class exercise {
	public static void main(String[] args) {
		double[] num = {1.0,2.0,3.0,4.0};
		double sum = 0;
		double max=num[0];
		for(double e:num) {
			System.out.print(e+ " ");
			sum += e;
			if(max<e) max = e;
		}
		System.out.println("\n합은 "+ sum);
		System.out.println("최대값은 "+ max);
    }
}

 

11.

public class exercise {
	public static void main(String[] args) {
		String[] s = {"Hello", "Java", "World"};
		for(String e:s) System.out.println(e);
    }
}

 

12.

public class exercise {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int sum =0;
		for(int i=0;i<5;i++) {
			System.out.print("성적을 입력하세요: ");
			int score = sc.nextInt();
			sum += score;
		}
		System.out.println("합계 : "+sum);
		System.out.println("평균 : "+sum/5);
    }
}
728x90
저작자표시 비영리 변경금지 (새창열림)

'Major > Java' 카테고리의 다른 글

[Power Java Compact] 4장 연습문제  (3) 2021.12.20
[Java] 문자열 안에서 단어찾기  (0) 2021.12.20
[Power Java Compact] 2장 연습문제  (2) 2021.12.13
[Power Java Compact] 1장 연습문제  (0) 2021.12.13
Power java 2판 - Chapter 2 exercise  (0) 2021.03.14
'Major/Java' 카테고리의 다른 글
  • [Power Java Compact] 4장 연습문제
  • [Java] 문자열 안에서 단어찾기
  • [Power Java Compact] 2장 연습문제
  • [Power Java Compact] 1장 연습문제
BeNI
BeNI
코딩하는 블로그
  • BeNI
    코딩못하는컴공
    BeNI
  • 전체
    오늘
    어제
    • Menu (254)
      • My profile (1)
      • 회고 | 후기 (8)
      • Frontend (66)
        • Article (11)
        • Study (36)
        • 프로그래머스 FE 데브코스 (19)
      • Backend (0)
      • Algorithm (58)
        • Solution (46)
        • Study (12)
      • Major (111)
        • C&C++ (23)
        • Java (20)
        • Data Structure (14)
        • Computer Network (12)
        • Database (15)
        • Linux (6)
        • Architecture (3)
        • Lisp (15)
        • OS (1)
        • Security (2)
      • etc (2)
  • 링크

    • 깃허브
    • 방명록
  • 인기 글

  • 최근 댓글

  • 최근 글

  • 태그

    프로그래머스
    react
    자료구조
    리팩토링
    Algorithm
    lisp
    백준
    데브코스
    C++
    파일처리
  • hELLO· Designed By정상우.v4.10.2
BeNI
[Power Java Compact] 3장 연습문제
상단으로

티스토리툴바