Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/sonchaeyi/report1/Report1_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//2-4. 다음 중 변수를 잘못 초기화 한 것은?

정답 :
byte b = 256;
char c = '';
char answer = 'no';
float f = 3.14
23 changes: 23 additions & 0 deletions src/sonchaeyi/report1/Report1_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//2-7. 다음 문장들의 출력 결과를 적으세요. 오류가 있는 문장의 경우, '오류' 라고 적으세요.

정답 :
System.out.println("1" + "2");
- 12

System.out.println(true+"");
- true

System.out.println('A' + 'B');
- 131

System.out.println('1' + 2);
- 51

System.out.println('1' + '2');
- 99

System.out.println('J' +"ava");
- Java

System.out.println(true + null);
- 오류
20 changes: 20 additions & 0 deletions src/sonchaeyi/report1/Report1_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//2-8. 아래는 변수 x, y, z의 값을 서로 바꾸는 예제이다. 결과와 같이 출력되도록 코드를 넣으세요.
public class Exercise2_8{
public static void main(String[] args){
int x = 1;
int y = 2;
int z = 3;

정답 :
int temp;
temp = x;
x = y;
y = z;
z = temp;

System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("z="+z);
}
}
//예상 결과 : x=2, y=3, z=1
9 changes: 9 additions & 0 deletions src/sonchaeyi/report2/Report2_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//3-1. 다음 중 형변환을 생략할 수 있는 것은? (모두 고르시오)
byte b = 10;
char ch = 'A';
int i = 100;
long l = 1000L;

정답 :
float f = (float)l;
i = (int)ch;
60 changes: 60 additions & 0 deletions src/sonchaeyi/report2/Report2_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//3-2. 다음 연산의 결과와 그 이유를 적으세요.
class Exercise3_2{
public static void main(String[] args){
int x = 2;
int y = 5;
char c = 'A'; // 'A'의 문자코드는 65

System.out.println(y >= 5 || x < 0 && x > 2);
- 결과 : true
&&가 ||보다 우선 순위가 높기 때문에 x < 0 && x > 2 먼저 처리.
x < 0 && x > 2 = false
y >= 5 = true
true || false = true

System.out.println(y += 10 - x++);
- 결과 : 13
y += 10는 y = y + 10과 같은 의미로 y = 15
x는 후위형으로 15 - 2를 한 후 3으로 증가

System.out.println(x += 2);
- 결과 : 5
위에서 x++로 x가 1 증가하여 3이 됨
증가한 값에 +2를 하면 5

System.out.println(!('A' <= c && c <= 'Z'));
- 결과 : false
'A'는 아스키코드표에 65이므로 c도 65, 'Z'는 90
65 <= 65 && 65 <= 90은 true
!는 not의 의미로 boolean의 값을 반대로 바꿈
true가 아닌 것은 false이므로 결과는 false

System.out.println('C' - c);
- 결과 : 2
'C'는 아스키코드표에서 67이고 c는 'A'이므로 67 - 65

System.out.println('5' - '0');
- 결과 : 5
'5'는 아스키코드표에서 53이고 '0'은 48
53 - 48 = 5

System.out.println(c + 1);
- 결과 : 66
c는 'A'고 'A'는 아스키코드표에서 65이기 때문에 +1을 하면 66

System.out.println(++c);
- 결과 : B
++c는 전위형이므로 1 증가 후 출력을 함
c는 'A'고 1 증가 후 66이되므로 'B'

System.out.println(c++);
- 결과 : B
c++는 후위형이므로 출력 후 1 증가함
위에 c가 1 증가해서 66 == 'B'가 출력되고
출력 후 1 증가함

System.out.println(c);
- 결과 : C
위에서 후위형으로 1 증가하여 66 + 1 = 67 = 'C'가 됨
}
}
9 changes: 9 additions & 0 deletions src/sonchaeyi/report2/Report2_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//3-3. 아래는 변수의 num 값 중에서 백의 자리 이하를 버리는 코드이다.
//만일 변수 num의 값이 '456'이라면 '400'이 되고, '111'이라면 '100'이 된다.
//알맞은 코드를 넣으시오.
class Exercise3_3 {
public static void main(String[] args){
int num = 456;
System.out.println((num / 100) * 100);
}
}
13 changes: 13 additions & 0 deletions src/sonchaeyi/report2/Report2_4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//3-4. 아래의 코드는 사과를 담는데 필요한 바구니(버켓)의 수를 구하는 코드이다.
//만일 사과의 수가 123개이고 하나의 바구니에는 10개의 사과를 담을 수 있다면, 13개의 바구니가 필요할 것이다.
//알맞은 코드를 넣으시오.
class Exercise3_4{
public static void main(String[] args){
int numOfApples = 123; // 사과의 개수
int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수)
int numOfBucket = Math.round((numOfApples / sizeOfBucket) + 0.5f); // 모든 사과를 담는데 필요한 바구니의 수

System.out.println("필요한 바구니의 수 :"+numOfBucket);
}
}
//예상 결과 -> 필요한 바구니의 수 :13
10 changes: 10 additions & 0 deletions src/sonchaeyi/report2/Report2_5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//3-5. 아래는 변수 num의 값에 따라 '양수', '음수', '0'을 출력하는 코드이다.
//삼항연산자를 이용해서 빈칸에 알맞은 코드를 넣으시오.
//Hint : 삼항 연산자를 두 번 사용할 것!
class Exercise3_5{
public static void main(String[] args){
int num = 10;
System.out.println(num > 0 ? "양수" : (num < 0 ? "음수" : "0"));
}
}
//예상 결과 : 양수
13 changes: 13 additions & 0 deletions src/sonchaeyi/report2/Report2_6
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//3-6. 아래는 화씨(Fahrenheit)를 섭씨(Celcius)로 변환하는 코드이다.
//변환 공식이 'C = 5/9*(F-32)'라고 할 때, 빈 칸에 알맞은 코드를 넣으시오.
// 단, 변환값은 소수점 셋째자리에서 반올림하며, Math.round() 함수를 사용하지 않고 처리할 것!
class Exercise3_6{
public static void main(String[] args){
int fahrenheit = 100;
float celcius = (/*빈 칸*/);

System.out.println("Fahrenheit:"+fahrenheit);
System.out.println("Celcius:"+celcius);
}
}
//예상 결과 : Fahrenheit:100, Celcius:37.78
25 changes: 25 additions & 0 deletions src/sonchaeyi/report3/Report3_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//4-1. 다음의 문장들을 조건식으로 표현해보세요.

//int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식
x > 10 && x < 20

//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식
!(ch == ' ' || ch == '\t')

//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식
ch == 'x' || ch == 'X'

//char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식
'0' <= ch && ch <= '9'

//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식
(ch <= 'a' && ch <= 'z') || (ch < = 'A' && ch <= 'Z')

//int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식
(year % 400 ==0 || year % 4 == 0) && year % 100 != 0

//boolean형 변수 powerOn이 false일 때 true인 조건식
powerOn == false

//문자열 참조변수 str이 "yes"일 때 true인 조건식
str.equals("yes")
34 changes: 34 additions & 0 deletions src/sonchaeyi/report3/Report3_10
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//4-10. 다음은 숫자맞추기 게임을 작성한 것이다. 1과 100사이의 값을 반복적으로 입력해서
//컴퓨터가 생각한 값을 맞추면 게임이 끝난다.
//사용자가 값을 입력하면, 컴퓨터는 자신이 생각한 값과 비교해서 결과를 알려준다.
//사용자가 컴퓨터가 생각한 숫자를 맞추면 게임이 끝나고 몇 번 만에 숫자를 맞췄는지 알려준다.

class Exercise4_14 {
public static void main(String[] args) {
// 1~100사이의 임의의 값을 얻어서 answer에 저장한다.
int answer = /*빈 칸*/;
int input = 0; //사용자입력을 저장할 공간
int count = 0; //시도횟수를 세기위한 변수

// 화면으로 부터 사용자입력을 받기 위해서 Scanner클래스 사용
java.util.Scanner s = new java.util.Scanner(System.in);
do {
count++;

System.out.print("1과 100사이의 값을 입력하세요 : ");

input = s.nextInt(); //입력받은 값을 변수 input에 저장한다.

if(answer > input) {
System.out.println("더 큰 수를 입력하세요.");
} else if(answer < input) {
System.out.println("더 작은 수를 입력하세요.");
} else {
System.out.println("맞혔습니다.");
break;
}
} while(true); //무한반복문

System.out.println("시도횟수는 " + count + "번입니다.");
} // end of main
} // end of class
13 changes: 13 additions & 0 deletions src/sonchaeyi/report3/Report3_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요.
class Exercise4_2 {
public static void main(String[] args) {
int sum = 0;

for(int i = 0; i < 20; i++) {
if(i % 2 != 0 && i % 3 != 0) {
sum += i;
}
}
System.out.println("sum="+sum);
}
}
13 changes: 13 additions & 0 deletions src/sonchaeyi/report3/Report3_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//4-3. 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+10)의 결과를 계산하세요.
class Exercise4_3 {
public static void main(String[] args) {
int sum = 0;
int totalSum = 0;

for (int i = 0; i < 10; i++) {
sum += i;
totalSum += sum;
}
System.out.println("totalSum="+totalSum);
}
}
21 changes: 21 additions & 0 deletions src/sonchaeyi/report3/Report3_4
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//4-4. 1+(-2)+3+(-4)+...과 같은 식으로 계속 더해나갔을 때,
//몇까지 더해야 총합이 100 이상이 되는지 구하세요.
class Exercise4_4 {
public static void main(String[] args) {
int sum = 0; // 총합을 저장할 변수
int s = 1; // 값의 부호를 바꿔주는데 사용할 변수
int num = 0;

for(int i = 1; true; i++, s =- s) {
num = s * i;
sum += num;

if(sum >= 100) {
break;
}
}

System.out.println("num="+num);
System.out.println("sum="+sum);
}
}
18 changes: 18 additions & 0 deletions src/sonchaeyi/report3/Report3_5
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//4-5. 다음의 for문을 while문으로 변경하세요.
public class Exercise4_5 {
public static void main(String[] args) {
int i = 0;

while (i < 11) {
int j = 0;

while (j <= i) {
System.out.printf("*");
j++;
}

System.out.println();
i++;
}
}//end of main
} // end of class
12 changes: 12 additions & 0 deletions src/sonchaeyi/report3/Report3_6
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//4-6. 두 개의 주사위를 던졌을 때, 눈의 합이 6이 되는 모든 경우의 수를 출력하는 프로그램을 작성하세요.
class Exercise4_6 {
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
if (i + j == 6) {
System.out.println(i + "+" + j + "=" + (i + j));
}
}
}
}
}
14 changes: 14 additions & 0 deletions src/sonchaeyi/report3/Report3_7
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//4-7. 숫자로 이루어진 문자열 str이 있을 때, 각 자리의 합을 더한 결과를 출력하는 코드를 완성하세요.
//만일 문자열이 "12345"라면, ‘1+2+3+4+5’의 결과인 15를 출력이 출력되어야 합니다.
class Exercise4_7 {
public static void main(String[] args) {
String str = "12345";
int sum = 0;

for (int i = 0; i < str.length(); i++) {
sum += str.charAt(i) - '0';
}

System.out.println("sum=" + sum);
}
}//예상 결과 : sum=15
7 changes: 7 additions & 0 deletions src/sonchaeyi/report3/Report3_8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//4-8. Math.random()을 이용해서 1부터 6 사이의 임의의 정수를 변수 value에 저장하는 코드를 완성하세요.
class Exercise4_8{
public static void main(String[] args){
int value = (int) ((Math.random() * 6) + 1);
System.out.println("value:"+value);
}
}
16 changes: 16 additions & 0 deletions src/sonchaeyi/report3/Report3_9
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//4-9. int 타입의 변수 num이 있을 때, 각 자리의 합을 더한 결과를 출력하는 코드를 완성하세요.
//만일 변수 num의 값이 12345라면, ‘1+2+3+4+5’의 결과인 15를 출력하세요.
//문자열로 변환하지 말고 숫자로만 처리하세요.
public class Exercise4_9 {
public static void main(String[] args) {
int num = 12345;
int sum = 0;

while(num > 0) {
sum += num % 10;
num /= 10;
}

System.out.println("sum="+sum);
}
}//예상 결과 : sum=15
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/sonchaeyi/report4/Report4_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//5-1. 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명하세요.
int[] arr = new int[5]{1,2,3,4,5};
- {}안의 데이터 개수에 따라 배열의 크기가 자동적으로 결정

int arr[5];
- 배열 선언할 때 크기를 지정 할 수 없음
9 changes: 9 additions & 0 deletions src/sonchaeyi/report4/Report4_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//5-2. 다음과 같은 배열이 있을 때, arr[3].length의 값은?
int[][]arr ={
{5,5,5,5,5},
{10,10,10},
{20,20,20,20},
{30,30}
};

- 답 : 2 ({30,30})
Loading