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
25 changes: 25 additions & 0 deletions src/LISHANYU/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인 조건식
ch >= '0' && ch <= '9'

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

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

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

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

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

// 화면으로 부터 사용자입력을 받기 위해서 Scanner클래스 사용
java.util.Scanner s = new java.util.Scanner(System.in);
do {
System.out.print("1과 100사이의 값을 입력하세요>");
input = s.nextInt(); //입력받은 값을 변수 input에 저장한다.
count++;
if (input > answer) {
System.out.println("더 작은 수를 입력하세요");
} else if (input < answer) {
System.out.println("더 큰 수를 입력하세요");
}
} while (input != answer); //무한반복문////

System.out.println("장답입니다.");
} // end of main
}
13 changes: 13 additions & 0 deletions src/LISHANYU/Report3/Report3_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package LISHANYU.Report3;
//4-2. 1부터 20까지의 정수중에서 2 또는 3의 배수가 아닌 수의 총합을 구하세요.
public class Report3_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);
}
}
14 changes: 14 additions & 0 deletions src/LISHANYU/Report3/Report3_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package LISHANYU.Report3;
//4-3. 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+10)의 결과를 계산하세요.
public class Report3_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.printf("i=%d sum=%d totalSum=%d%n", i, sum, totalSum);
}
System.out.println("totalSum="+totalSum);
}
}
23 changes: 23 additions & 0 deletions src/LISHANYU/Report3/Report3_4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package LISHANYU.Report3;
//4-4. 1+(-2)+3+(-4)+...과 같은 식으로 계속 더해나갔을 때,
//몇까지 더해야 총합이 100 이상이 되는지 구하세요.
public class Report3_4 {
public static void main(String[] args) {
int sum = 0; // 총합을 저장할 변수
int s = 1; // 값의 부호를 바꿔주는데 사용할 변수
int num = 0;
for (int i=1;sum<=100;i++){
if (i % 2 !=0 ){
s= Math.abs(s);
}else{
s= -s;
}
num = s * i;
sum += num;
// System.out.printf("i=%d s=%d num=%d sum=%d%n",i, s, num, sum);
}
System.out.println("num="+num);
System.out.println("sum="+sum);
}

}
22 changes: 22 additions & 0 deletions src/LISHANYU/Report3/Report3_5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package LISHANYU.Report3;
//4-5. 다음의 for문을 while문으로 변경하세요.
public class Report3_5 {
public static void main(String[] args) {
int i=0;// 초기화
while (i<=10){ //조건식
int j=0;
while (j<=i){
j++;
System.out.print("*");
}
i++;
System.out.println();
}

// 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
13 changes: 13 additions & 0 deletions src/LISHANYU/Report3/Report3_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package LISHANYU.Report3;
//4-6. 두 개의 주사위를 던졌을 때, 눈의 합이 6이 되는 모든 경우의 수를 출력하는 프로그램을 작성하세요.
public class Report3_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.printf("%d + %d = %d%n", i, j ,i+j);
}//if문 end
}// j for end
}// i for end
} // main end
} //class end
17 changes: 17 additions & 0 deletions src/LISHANYU/Report3/Report3_7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package LISHANYU.Report3;
//4-7. 숫자로 이루어진 문자열 str이 있을 때, 각 자리의 합을 더한 결과를 출력하는 코드를 완성하세요.
//만일 문자열이 "12345"라면, ‘1+2+3+4+5’의 결과인 15를 출력이 출력되어야 합니다.
public class Report3_7 {
public static void main(String[] args) {
String str = "12345";
int sum = 0;
int num = Integer.parseInt(str);
for (int i = 0; i < (str.length()); i++) {
int a = num%10;
sum += a;
num = num/10;
// System.out.printf("i=%d num=%d a=%d sum=%d%n",i,num,a,sum);
}
System.out.println("sum=" + sum);
}
}
9 changes: 9 additions & 0 deletions src/LISHANYU/Report3/Report3_8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package LISHANYU.Report3;
//4-8. Math.random()을 이용해서 1부터 6 사이의 임의의 정수를 변수 value에 저장하는 코드를 완성하세요.
public class Report3_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/LISHANYU/Report3/Report3_9.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package LISHANYU.Report3;
//4-9. int 타입의 변수 num이 있을 때, 각 자리의 합을 더한 결과를 출력하는 코드를 완성하세요.
//만일 변수 num의 값이 12345라면, ‘1+2+3+4+5’의 결과인 15를 출력하세요.
//문자열로 변환하지 말고 숫자로만 처리하세요.
public class Report3_9 {
public static void main(String[] args) {
int num = 12345;
int sum = 0;
while (num>0) {
int a = num % 10;
sum += a;
num = num / 10;
System.out.println("sum=" + sum);
}
}
}
16 changes: 16 additions & 0 deletions src/LISHANYU/Report4/Report4_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//5-1. 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명하세요.
int[] arr[]; 정답: 2차원 배열의 선언

int[] arr = {1,2,3,}; 정답

int[] arr = new int[5]; 정답

int[] arr = new int[5]{1,2,3,4,5};
수정: int[] arr = new int[]{1,2,3,4,5};
이유: 두번째 []에는 숫자를 넣으면 안된다.{}안의 값의 개수에 따라 자동으로 배정의 길이를 결정된다.

int arr[5];
틀림: 배열을 선언할 때 배열의 크기를 지정할 수 없다.

int[] arr[] = new int[3][]; 정답:

13 changes: 13 additions & 0 deletions src/LISHANYU/Report4/Report4_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package LISHANYU.Report4;
//5-2. 다음과 같은 배열이 있을 때, arr[3].length의 값은?
public class Report4_2 {
public static void main(String[] args) {
int[][] arr = {
{5, 5, 5, 5, 5},
{10, 10, 10},
{20, 20, 20, 20},
{30, 30}
};
System.out.println(arr.length); // 답안은 4
}
}
13 changes: 13 additions & 0 deletions src/LISHANYU/Report4/Report4_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package LISHANYU.Report4;
//5-3. 배열 arr에 담긴 모든 값을 더하는 프로그램을 완성하세요.
public class Report4_3 {
public static void main(String[] args){
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;

for (int i=0; i<arr.length;i++){
sum += arr[i];
}
System.out.println("sum="+sum);
}
}
29 changes: 29 additions & 0 deletions src/LISHANYU/Report4/Report4_4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package LISHANYU.Report4;
//5-4. 2차원 배열 arr에 담긴 모든 총합과 평균을 구하는 프로그램을 완성하세요.
public class Report4_4 {
public static void main(String[] args) {
int[][] arr = {
{ 5, 5, 5, 5, 5 },
{ 10, 10, 10, 10, 10 },
{ 20, 20, 20, 20, 20 },
{ 30, 30, 30, 30, 30 }
};


int total = 0;
float average = 0;

for (int i=0; i < arr.length; i++){
for (int j=0; j <arr[i].length; j++){
total += arr[i][j];
}
}
average = total/(float)(arr.length*arr[0].length);
System.out.println("total=" + total);
System.out.println("average=" + average);

}
/*빈 칸*/


}
28 changes: 28 additions & 0 deletions src/LISHANYU/Report4/Report4_5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package LISHANYU.Report4;
//5-5. 다음은 1과 9 사이의 중복되지 않은 숫자로 이루어진 3자리 숫자를 만들어내는 프로그램이다.
//코드를 완성하세요. 다만 Math.random()을 사용했기 때문에 실행 결과 예시와 다를 수 있습니다.
public class Report4_5 {
public static void main(String[] args) {
int[] ballArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] ball3 = new int[3];

// 배열 ballArr의 임의의 요소를 골라서 위치를 바꾼다
for (int i = 0; i < ballArr.length; i++) {
int j = (int) (Math.random() * ballArr.length);
int tmp = 0;
/*빈 칸*/
tmp = ballArr[i];
ballArr[i]=ballArr[j];
ballArr[j]=tmp;

}
for (int i = 0; i < 3; i++) {
ball3[i] = ballArr[i];
}
// 배열 ballArr의 앞에서 3개의 수를 배열 ball3로 복사한다
System.arraycopy(ballArr,0,ball3,0,3);
for (int i = 0; i < ball3.length; i++) {
System.out.print(ball3[i]);
}
}//end of main
}
29 changes: 29 additions & 0 deletions src/LISHANYU/Report4/Report4_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package LISHANYU.Report4;
//5-6. 단어의 글자위치를 섞어서 보여주고 원래의 단어를 맞추는 예제이다.
//실행결과와 같이 동작하도록 빈 칸을 채우세요.
public class Report4_6 {
public static void main(String args[]) {
String[] words = { "television", "computer", "mouse", "phone" };

java.util.Scanner scanner = new java.util.Scanner(System.in);

for (int i = 0; i < words.length; i++) {
char[] question = words[i].toCharArray(); // String을 char[]로 변환
for (int j = 0; j < question.length; j++){
int a = (int) (Math.random() * question.length);

char tmp = question[i];
question[i]=question[a];
question[a] = tmp;
}
System.out.printf("Q%d. %s의 정답을 입력하세요.>", i + 1, new String(question));
String answer = scanner.nextLine();

// trim()으로 answer의 좌우 공백을 제거한 후, equals로 word[i]와 비교
if (words[i].equals(answer.trim()))
System.out.printf("맞았습니다.%n%n");
else
System.out.printf("틀렸습니다.%n%n");
}
} //end of main
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/LISHANYU/Report5/Report5_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package LISHANYU.Report5;
//6-1. 다음과 같은 멤버 변수를 갖는 Student 클래스를 정의하세요.
//타입 : String, 변수명 : name, 설명 : 학생 이름
//타입 : int, 변수명 : ban, 설명 : 반
//타입 : int, 변수명 : no, 설명 : 번호
//타입 : int, 변수명 : kor, 설명 : 국어 점수
//타입 : int, 변수명 : eng, 설명 : 영어 점수
//타입 : int, 변수명 : math, 설명 : 수학 점수
public class Report5_1 {

String name;
int ban;
int no;
int kor;
int eng;
int math;


}
35 changes: 35 additions & 0 deletions src/LISHANYU/Report5/Report5_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package LISHANYU.Report5;
//6-2. 6-1에서 정의한 Student 클래스에 생성자와 info()를 추가해서 실행결과와 같은 결과를 얻도록 하세요.
public class Report5_2 {
public static void main(String[] args) {
Student s = new Student("홍길동", 1, 1, 100, 60, 76);

String str = s.info();
System.out.println(str);
}// 예상 결과 : 홍길동, 1, 1, 100, 60, 76, 236, 78.7
}

class Student {
String name;
int ban;
int no;
int kor;
int eng;
int math;

// Student() {}
Student(String name, int ban, int no, int kor, int eng, int math) {//생성자
this.name = name;
this.ban = ban;
this.no = no;
this.kor = kor;
this.eng = eng;
this.math = math;
}

public String info() {
return name + "," + ban + "," + no + "," + kor + "," + eng + "," + math
+ "," + (kor+eng+math)+ "," + ((int)((kor+eng+math)/3f*10)/10f );
}
}

Loading