19/12/31 2일차 제어문 if / 반복문 for / Class 개념 기본구조
교육 2일차
제어문 if / 반복문 for / Class 개념 기본구조
제어문 if / 반복문 for 연습
public static void main(String[] args) {
String[] code={"JP", "FR", "JP", "US", "CN", "DE", "KR", "JP", "DE","KR"};
int[] age={27,34,28,26,41,28,42,29,29,32};
System.out.print("The length of CODE is : ");
System.out.println(code.length);
// if exercises
int count = 0;
if(code[0].equals("JP")){
count = count +1 ;
}
if(code[1].equals("JP")){
count = count +1 ;
}
if(code[2].equals("JP")){
count = count +1 ;
}
if(code[3].equals("JP")){
count = count +1 ;
}
if(code[4].equals("JP")){
count = count +1 ;
}
if(code[5].equals("JP")){
count = count +1 ;
}
if(code[6].equals("JP")){
count = count +1 ;
}
if(code[7].equals("JP")){
count = count +1 ;
}
if(code[8].equals("JP")){
count = count +1 ;
}
if(code[9].equals("JP")){
count = count +1 ;
}
System.out.print("The number of JP is : " + count);
// if-else exercise
int a = 10; int b = 15;
if(a>b){
System.out.println(a+b);
}
else{
System.out.println("false");
}
// 'for' exercise
count = 0;
for(int i=0; i<code.length; i++){
if(code[i].equals("JP")){
count++;
}
}
System.out.print("The number of JP is : " + count);
// variable declaration 'local'
count = 0;
String local = "JP";
for(int j=0; j<code.length;j++){
if(code[j].equals(local)){
count++;
}
}
System.out.print("The Number of local JP is : " + count);
// 'for' exercise : sum of ages
int sum = 0;
for(int k=0; k<age.length; k++){
sum = sum + age[k];
}
System.out.print("The sum of ages is : " + sum);
// 'for-if' exercise : The Sum of ages in KR
int kragesum = 0;
for(int m=0; m<age.length; m++){
if(code[m].equals("KR")){
kragesum = kragesum + age[m];
}
}
System.out.print("The Sum of ages in KR is : " + kragesum);
}
| cs |
for 중첩 연습 : Multiplicaton
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public static void main(String[] args) {
int n, k; int r = 0;
System.out.println("Multiplication\n");
System.out.println("--------------");
for(n = 1; n <10 ; n++){
System.out.println("\n< " + n + " multiplication >\n");
for(k = 1; k < 10 ; k++){
r = n * k;
System.out.println(n + "*" + k + "=" + r);
if(k == 9) {
System.out.println("\n--------------");
}
}
}
}
| cs |
Basic Structure of Class
Main Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public class MainClass {
public static void main(String[] args) {
// 일반 class 불러와서 데이터 입력
BookClass b1 = new BookClass();
b1.author = "Wiii";
b1.price = 250;
b1.realPrice(120);
System.out.println("Real Price of the Book1 is : " + b1.r_price);
BookClass b2=new BookClass();
b2.author = "YEJI";
b2.price = 300;
b2.realPrice(100);
System.out.println("Real Price of the Book2 is : " + b2.r_price);
// 배열 만들기
BookClass[] books = new BookClass[2];
books[0] = b1;
books[1] = b2;
// BookClass[] books2 = {b1, b2};
System.out.println("Length of books[] is : " + books.length);
System.out.println("Real Price of the book1 is : " + books[0].r_price);
System.out.println("The author of the book2 is : " + books[1].author);
// exercising class array and for
for(int i=0;i<books.length;i++){
System.out.println("The author of the book" + (i+1) + "is : " + books[i].author);
System.out.println("Real Price of the book" + (i+1) + "is : " + books[i].r_price);
}
//
System.out.println(books[0].author + " || " + books[1].author);
}
| cs |
일반 Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class BookClass {
String author="";
int price = 0;
int r_price = 0; // real price
public BookClass(){ // 기본 생성자
}
public void realPrice(int d){ // 사용자 정의 method
r_price = price - d;
}
}
| cs |
응용해보자
for + switch : Multiplication
public static void main(String[] args) {
int i, j, k=0;
System.out.println("It's Table of Multiplication\n");
System.out.println("***************************\n");
for(i=1; i<10; i++) {
System.out.println("These are multiples of " + i + ".\n");
for(j=1; j<10; j++) {
k = i * j;
System.out.println(i + " multipy " + j + " is " + k + ".");
}
switch(i) {
case 1 : System.out.println("\n1산동\n"); break;
case 2 : System.out.println("\n2천시\n"); break;
case 3 : System.out.println("\n3척군\n"); break;
case 4 : System.out.println("\n4천시\n"); break;
case 5 : System.out.println("\n5산시\n"); break;
case 6 : System.out.println("\n6은 뭐가 있을까~\n"); break;
case 7 : System.out.println("\n7포해수욕장\n"); break;
case 8 : System.out.println("\n8각정이 있는 곳은 어딜까~\n"); break;
case 9 : System.out.println("\n9룡포\n"); break;
}
System.out.println("***************************\n");
}
System.out.println("FINISH!");
}
| cs |
댓글
댓글 쓰기