JAVA

<JAVA_231208금> for문, array, 구구단, 로또, 가위바위보

Technoqueen_X 2023. 12. 11. 13:38
728x90
반응형

객체지향언어 : 메모리에 올려놓지 않으면 못 씀.

 

여러개의 프로젝트를 합칠 때 동일한 이름의 자바 소스파일이 있어도 패키지가 분리되어 있으면(=구분되면) 충돌X

 

1개의 소스파일에 여러개 클래스가 존재 가능한데,

몇개의 클래스가 존재하든 최대 1개의 클래스만 public 포함 가능한 이유는

소스파일명은 반드시 클래스명과 같아야 하는데,

public이 붙은 클래스를 소스파일명으로 해야하기 때문.

 

<배열>

같은 datatype(기본형, 참조형...) 을 일괄 처리할 때 사용되는 객체

ex) int[] b; 또는 int b[]; 인데 앞에형식을 선호한다.

 

 

<cmd에서 class 실행하는 방법>

 

 

---------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------

 

23-12-10(일) 숙제제출_이수정

1. 10,9,8,7,6,5,4,3,2,1 순으로 정수를 출력하는 프로그램을 작성하시오.

// 풀이 : 
for(int i=10; 1<=i && i<=10; i--)
System.out.println(i);



2. 다음의 프로그램을 실행하면 어떤 결과가 출력될까요? 또 키위('K')가 출력되도록 수정하시오.

class FruitVendingMachine {
public static void main String args[]) {
char coin = ‘K’;
switch (coin) {
case ‘A’ :
System.out.println(“사과”);
break;
case ‘P’ :
System.out.println(“배”);
break;
case ‘G’ :
System.out.println(“포도”);
break;
default :
System.out.println(coin);
break;
}
}
}

// 풀이(1) :
K



// 풀이(2) :
char coin = 'K';

switch (coin) {
case 'A' :
System.out.println("사과");
break;

case 'P' :
System.out.println("배");
break;

case 'G' :
System.out.println("포도");
break;

case 'K' :
System.out.println("키위");
break;

default :
System.out.println(coin);
break;
}



(응용문제)
2.1. 모든 과일이 선택되어 나오도록 수정하시오.

// 풀이 :
char coin = 'F';

switch (coin) {
case 'A' :
System.out.println("사과");
break;
case 'P' :
System.out.println("배");
break;
case 'G' :
System.out.println("포도");
break;
case 'K' :
System.out.println("키위");
break;
default : 
System.out.println("사과 배 포도 키위");
break;
}

 


2.2. 대/소문자 상관없이 p(P)를 입력하면 '배'가 선택되도록 클래스를 작성하시오.

// 풀이 :
char coin = 'p';

switch (coin) {
case 'A' :
System.out.println("사과");
break;

case 'p' : case 'P' :
System.out.println("배");
break;

case 'G' :
System.out.println("포도");
break;

case 'K' :
System.out.println("키위");
break;

default :
System.out.println(coin);
break;
}



2.3. 다음의 machine 메서드를 구현해 보시오.(가능한 사람만!!-조장/반장 필!)
class FruitVendingMachine{
public static void main(String args[]){
char coin = 'K';
String str;
str=machine(coin); // 메소드 호출
System.out.println("자판기 선택 과일 : " + str);
}
static String machine.... // 메소드 선언*** 구현하기
..... }

// 풀이 : 
char coin = 'K';

        String str;
        str = machine(coin);  // 메서드 호출
        System.out.println("자판기 선택 과일 : " + str);
    }

static String machine(char coin) {
switch (coin) {
case 'A' :
System.out.println("사과");
break;

case 'p' :
case 'P' :
System.out.println("배");
break;

case 'G' :
System.out.println("포도");
break;

case 'K' :
System.out.println("키위");
break;

default :
System.out.println(coin);
break;
}
return String.valueOf(coin);



3. switch문과 if문의 상호 변환관계에 대해 설명하시오.(간단한 예로 코딩하기)

// 풀이 : 
// switch문 
int a = 10;

switch(a) {
case 10 :
System.out.println("A");
break;
case 9 :
System.out.println("B");
break;
default :
System.out.println("F");
}



// if문
int a=10;

if (a>=10) {
System.out.println("A");
} else if (a<=9) {
System.out.println("B");
} else {
System.out.println("F");
}



4. 다음의 프로그램은 잘못된 프로그램입니다. 어떤 부분이 잘못되었는지 찾고, 잘못된 이유를 설명하십시오.
class Total {
public static void main String args[]) {
int total = 0;
for (byte cnt=0; cnt<200; cnt++)
total += cnt;
System.out.println(total);
}
}

// 풀이 : 
int total = 0;
for (int cnt=0; cnt<200; cnt++)
total += cnt;
System.out.println(total);



5. 다음 형식으로 출력되도록 프로그램하시오.
1까지의 합 : 1
2까지의 합 : 3
3까지의 합 : 6
 .....
10까지의 합 : 55

// 풀이 : 
  int sum = 0;
  
  for (int i=1; i<=10; i++) {
  sum += i;
  System.out.println("합 "+sum);
  }

 


6. 임의로 발생된 10~100점 사이의 점수로 다음의 메세지를 출력하기 위한 프로그램을 작성하시오.(switch)
   점수가 100점이면 선물이 자전거, TV, 노트북, 냉장고, 만년필입니다.
           90점이면 선물이 TV, 노트북, 냉장고, 만년필입니다.
   80점이면 선물이 노트북, 냉장고, 만년필입니다.
   70점이면 선물이 냉장고, 만년필입니다.
   그외 점수이면 선물이 만년필입니다.
   출력예) 당신의 점수는 80이고, 선물은 노트북, 냉장고, 만년필입니다.

// 풀이 :
int[] jumsu = new int[100];

for (int i=1; i<jumsu.length; i++) {
jumsu[i] = i+1;
}

for (int i=10; i<=1000; i++) {

int com = (int)(Math.random()*100);

int room = jumsu[0];
jumsu[0] =  jumsu[com];
jumsu[com] = room;
}

for (int i=0; i<=0; i++) {
System.out.print("당신의 점수는 " + jumsu[i] + "점 입니다. \n");
}

Scanner sc = new Scanner(System.in);
System.out.print("선물을 받으려면 점수를 입력하세요: ");
int score = sc.nextInt();

switch(score) {
case 91 : case 92 : case 93 : case 94 : case 95 : case 96 : case 97 : case 98 : case 99 : case 100 :
System.out.println("선물이 자전거, TV, 노트북, 냉장고, 만년필 입니다.");
break;
case 81 : case 82 : case 83 : case 84 : case 85 : case 86 : case 87 : case 88 : case 89 : case 90 :
System.out.println("선물이 TV, 노트북, 냉장고, 만년필 입니다.");
break;
case 71 : case 72 : case 73 : case 74 : case 75 : case 76 : case 77 : case 78 : case 79 : case 80 :
System.out.println("선물이 노트북, 냉장고, 만년필 입니다.");
break;
case 70 :
System.out.println("선물이 냉장고, 만년필 입니다.");
break;

default :
System.out.println("선물이 만년필 입니다.");
}

 


7. 임의의 단을 발생하여 그 단을 출력하는 프로그램을 작성하시오. (for)
   단, 1 또는 10인 경우 출력에서 제외!

// 풀이 :

int[] gugudan = new int[9];

for (int i=1; i<gugudan.length; i++) {
gugudan[i] = i+1;
}

for (int i=0; i<=1000; i++) {

int com = (int)(Math.random()*9);

int room = gugudan[0];
gugudan[0] =  gugudan[com];
gugudan[com] = room;
}

for (int i=0; i<=0; i++) {
System.out.print(gugudan[i] + "단을 계산하세요 \n");
}

Scanner sc = new Scanner(System.in);
System.out.print("단수 입력 : ");

int dan = sc.nextInt();

for (int i=1; i<=9; i++) {
System.out.println(dan + "x" + i + "=" + dan * i );
}

 


8. 임의의 수를 발생하여 그 수만큼 다음의 형태로 '*'을 출력하는 프로그램을 작성하시오.(do~while)
******   (6인 발생된 경우)
*****  
****     
***       
**         
*

// 풀이 : 
int[] su = new int[10];

for (int i=0; i<su.length; i++) {
su[i] = i+1;
}

for (int i=0; i<=1000; i++) {

int com = (int)(Math.random()*10);

int room = su[0];
su[0] = su[com];
su[com] = room;
}

for (int i=0; i<=0; i++) {
System.out.print(su[i] + "만큼 *을 찍으세요 \n");
}

Scanner sc = new Scanner(System.in);
System.out.print("별 개수 입력 : ");

int star = sc.nextInt();

        int i = star;
        do {
            if (i > 0) {
                for (int j=0; j< i; j++) {
                    System.out.print("*");
                }
                System.out.println();
            } 
            i--;
        } while (i >= 1);    

 


9. 다음의 형태로 출력되는 프로그램을 작성하시오.(while)
10
9
8
7
...
1

// 풀이 : 
        int i = 10;
        do {
            System.out.println(i);
            i--;
        } while (i >= 1);



10.1~30 사이의 숫자를 출력하되 3의 배수는 제외하고 출력하는 프로그램을 작성하시오.(반복문 아무거나)
1 2 4 5 7 8 10 .... 29

// 풀이 :
 for (int i=1; i<=30; i++) {
            if (i%3 != 0) {
                System.out.println(i);
            }
        }

 


11.1+(-2)+3+(-4)+... 과 같은 식으로 계속 더해나갔을 때, 몇까지 더해야 총합이 100이상이 되는지 구하시오.

// 풀이 :
int sum = 0;
int num = 1;

while (true) {
if (num%2 != 0) {
     sum += num;
    } else {
     sum += num * -1;
    }

    if (sum >= 100){
     System.out.println("정답은 : " + num);
     break;
    }
    num++;
}

 


12. 다이아몬드로 별을 출력하는 프로그램을 작성하시오(중간 9개 최대) - for문 // 조장, 반장 필!
     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *

// 풀이 :
 int i, j;
 
        for (i=0; i<=8; i++) {
            for (j=0; j<=8-i; j++) {
                System.out.print(" ");
            }
            for (j=0; j<=i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        
        for (i=7; i>=0; i--) {
            for (j=0; j<=8-i; j++) {
                System.out.print(" ");
            }
            for (j=0; j<=i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

728x90
반응형