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
8 changes: 8 additions & 0 deletions src/geunhokim/report1/Report 1_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//2-4. 다음 중 변수를 잘못 초기화 한 것은?
1. byte b = 256;
2. char c = '';
3. char answer = 'no';
4. float f = 3.14
5. double d = 1.4e3f;

=> 1,2,3,4
8 changes: 8 additions & 0 deletions src/geunhokim/report1/Report 1_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//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); => 오류
21 changes: 21 additions & 0 deletions src/geunhokim/report1/Report1_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package geunhokim.report1;

//2-8. 아래는 변수 x, y, z의 값을 서로 바꾸는 예제이다. 결과와 같이 출력되도록 코드를 넣으세요.
public class Report1_3 {
public static void main(String[] args){
int x = 1;
int y = 2;
int z = 3;

int a;
a = x;
x = y;
y = z;
z = a;

System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("z="+z);
}
}
//예상 결과 : x=2, y=3, z=1
13 changes: 13 additions & 0 deletions src/geunhokim/report2/Report 2_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
byte b = 10;
char ch = 'A';
int i = 100;
long l = 1000L;

//3-1. 다음 중 형변환을 생략할 수 있는 것은? (모두 고르시오)
1. b = (byte)i;
2. ch = (char)b;
3. short s = (short)ch;
4. float f = (float)l;
5. i = (int)ch;

=> 2번, 5번
39 changes: 39 additions & 0 deletions src/geunhokim/report2/Report2_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package geunhokim.report2;

public class Report2_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);
// ->정답 : false, 이유 : &&는 양쪽 다 같아야 하는데, 왼쪽은 true 오른쪽은 false이기 때문에
// System.out.println(y += 10 - x++);
// ->정답 : 13, 이유 : 5 + 10 - 2 = 13 한 후, 결과에 관계없이 x값을 -1하니까 최종 값에는 변화가 없다.
//
// System.out.println(x += 2);
// -> 정답 : 5, 이유 : 2 + (1+2) = 5
//
// System.out.println(!('A' <= c && c <= 'Z'));
// -> 정답 : false, 이유 : !('A' <= 'A' && 'A' <= 'Z')는 !(true && true)이고 따라서 false이다
//
// System.out.println('C' - c);
// -> 정답 : 2, 이유 : 67 - 65 = 2
//
// System.out.println('5' - '0');
// -> 정답 : 5
//
// System.out.println(c + 1);
// -> 정답 : 66, 이유 : 65 + 1 = 66
//
// System.out.println(++c);
// -> 정답 : 'B'
//
// System.out.println(c++);
// -> 정답 : 'B'
//
// System.out.println(c);
// -> 정답 : 'C'
}

}
12 changes: 12 additions & 0 deletions src/geunhokim/report2/Report2_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package geunhokim.report2;

//3-3. 아래는 변수의 num 값 중에서 백의 자리 이하를 버리는 코드이다.
//만일 변수 num의 값이 '456'이라면 '400'이 되고, '111'이라면 '100'이 된다.
//알맞은 코드를 넣으시오.

public class Report2_3 {
public static void main(String[] args){
int num = 456;
System.out.println(num / 100 * 100);
}
}
16 changes: 16 additions & 0 deletions src/geunhokim/report2/Report2_4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package geunhokim.report2;

//3-4. 아래의 코드는 사과를 담는데 필요한 바구니(버켓)의 수를 구하는 코드이다.
//만일 사과의 수가 123개이고 하나의 바구니에는 10개의 사과를 담을 수 있다면, 13개의 바구니가 필요할 것이다.
//알맞은 코드를 넣으시오.
public class Report2_4 {
public static void main(String[] args){
int numOfApples = 123; // 사과의 개수
int sizeOfBucket = 10; // 바구니의 크기(바구니에 담을 수 있는 사과의 개수)
int numOfBucket = numOfApples/sizeOfBucket + (numOfApples%sizeOfBucket > 0 ? 1 : 0); // 모든 사과를 담는데 필요한 바구니의 수

System.out.println("필요한 바구니의 수 :"+numOfBucket);
}
}

//예상 결과 -> 필요한 바구니의 수 :13
14 changes: 14 additions & 0 deletions src/geunhokim/report2/Report2_5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package geunhokim.report2;

//3-5. 아래는 변수 num의 값에 따라 '양수', '음수', '0'을 출력하는 코드이다.
//삼항연산자를 이용해서 빈칸에 알맞은 코드를 넣으시오.
//Hint : 삼항 연산자를 두 번 사용할 것!
public class Report2_5 {
public static void main(String[] args){
int num = 10;
String result = (num>=0)?(num>0)?"양수":"0":"음수";
System.out.println(result);
}
}

//예상 결과 : 양수
16 changes: 16 additions & 0 deletions src/geunhokim/report2/Report2_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package geunhokim.report2;

//3-6. 아래는 화씨(Fahrenheit)를 섭씨(Celcius)로 변환하는 코드이다.
//변환 공식이 'C = 5/9*(F-32)'라고 할 때, 빈 칸에 알맞은 코드를 넣으시오.
// 단, 변환값은 소수점 셋째자리에서 반올림하며, Math.round() 함수를 사용하지 않고 처리할 것!
public class Report2_6 {
public static void main(String[] args){
int fahrenheit = 100;
float celcius = (int)((5 / 9f * (fahrenheit - 32)) * 100 + 0.5) / 100f;

System.out.println("Fahrenheit:"+fahrenheit);
System.out.println("Celcius:"+celcius);
}
}

//예상 결과 : Fahrenheit:100, Celcius:37.78
Binary file added src/geunhokim/report3/report.login.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions src/geunhokim/report3/report3_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//4-1. 다음의 문장들을 조건식으로 표현해보세요.

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

if(10 < x && x < 20) {
}

//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식

if(ch != '' || ch != "\t") {
}

//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식
if(ch.equals('x') || ch.equals('X')) {
}

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

if(ch > '0' && ch < '9') {
}

//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식

if('a' < ch < 'z' || 'A' < ch < 'Z') {
}

//int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식

if(year%400 = 0 || (year%4 = 0 && year%100 != 0)) {
}

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

if(powerOn == false) {
}

//문자열 참조변수 str이 "yes"일 때 true인 조건식

if(str.equals("yes")) {
}
46 changes: 46 additions & 0 deletions src/geunhokim/report3/report3_10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package geunhokim.report3;

//4-10. 다음은 숫자맞추기 게임을 작성한 것이다. 1과 100사이의 값을 반복적으로 입력해서
//컴퓨터가 생각한 값을 맞추면 게임이 끝난다.
//사용자가 값을 입력하면, 컴퓨터는 자신이 생각한 값과 비교해서 결과를 알려준다.
//사용자가 컴퓨터가 생각한 숫자를 맞추면 게임이 끝나고 몇 번 만에 숫자를 맞췄는지 알려준다.

class Exercise4_10 {
public static void main(String[] args) {
// 1~100사이의 임의의 값을 얻어서 answer에 저장한다.
int answer = (int)(Math.random()*99+1);
System.out.println(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("더 작은 수를 입력하세요");
}

} while(true); //무한반복문
} // end of main
} // end of class
//예상 결과
//1과 100사이의 값을 입력하세요 : 50
//더 큰 수를 입력하세요.
//1과 100사이의 값을 입력하세요 : 75
//더 큰 수를 입력하세요.
//1과 100사이의 값을 입력하세요 : 87
//더 작은 수를 입력하세요.
//1과 100사이의 값을 입력하세요 : 80
//더 작은 수를 입력하세요.
//1과 100사이의 값을 입력하세요 : 77
//더 작은 수를 입력하세요.
//1과 100사이의 값을 입력하세요 : 76
//맞혔습니다.
//시도횟수는 6번입니다.
15 changes: 15 additions & 0 deletions src/geunhokim/report3/report3_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package geunhokim.report3;

//4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요.
class Exercise4_2 {
public static void main(String[] args) {
int sum = 0;

for(int i = 1; i <= 20; i++) {
if((i%2 != 0 && i%3 != 0)) {
sum += i;
}
}
System.out.println("sum="+sum);
}
}
16 changes: 16 additions & 0 deletions src/geunhokim/report3/report3_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package geunhokim.report3;

//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 = 1; i <= 10; i++) {
sum += i;
totalSum += sum;
}

System.out.println("totalSum="+totalSum);
}
}
33 changes: 33 additions & 0 deletions src/geunhokim/report3/report3_4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package geunhokim.report3;

//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;
int i = 1;

while (sum <= 100) {
if (i % 2 == 0) {
s = -1;
num = s * i++;
sum += num;
} else {
s = 1;
num = s * i++;
sum += num;
}

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

System.out.println("num="+num);
System.out.println("sum="+sum);

}

}
38 changes: 38 additions & 0 deletions src/geunhokim/report3/report3_5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package geunhokim.report3;

//4-5. 다음의 for문을 while문으로 변경하세요.
class Exercise4_5 {
public static void main(String[] args) {
for(int i=0; i<=10; i++) {
for(int j=0; j<=i; j++)
System.out.print("*");
System.out.println();
}
}//end of main
} // end of class

class Exercise4_5_1 {
public static void main(String[] args) {
int i = 0, j = 0;

while (i <= 10) {

j = 0;

while (j <= i) {

System.out.print("*");

j++;

}

System.out.println();

i++;

}

}
}

19 changes: 19 additions & 0 deletions src/geunhokim/report3/report3_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package geunhokim.report3;


//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 + ")");
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/geunhokim/report3/report3_7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package geunhokim.report3;

//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
Loading