본문 바로가기
코딩/자바 기초특강

20.11.06 FRI 자바기초특강 5

by 6^6 2020. 11. 6.
728x90

<목차>

  1. 어제과제 풀이

 

 

 

 

/*
Q1. 다음을 출력하세요.
★★★★
★★★★
★★★★
★★★★

Q2. 다음을 출력하세요.
1 2 3
4 5 6
7 8 9

 

Q3. 다음을 출력하세요.
1 2 3
2 3 4
3 4 5

Q4. 다음을 출력하세요.

★★
★★★

Q5. 다음을 출력하세요.
1
2 3
4 5 6

Q6. 다음을 출력하세요.
1
1 2
1 2 3

Q7. 구구단을 출력하시오
2x1=2 3x1=3 4x1=4....
2x2=4 3x2=6 4x2=8....
2x3=6 3x3=9 4x3=12....
....

 

*/

 

class Ex31{

public static void main(String[] args){


System.out.println("Q1");

for(int i1=1; i1<5; i1++){
for(int j1=1; j1<4; j1++){
System.out.print("★");
}
System.out.println(); //여기에 별넣어서 4x4된것. System.out.println("★");
}

/*다른패턴
//****(엔터)****(엔터)****
System.out.println();
int gap=1;
for(int i=0; i<20; i++){
if(gap!=5){ //엔터는 5번째에서. 5도 따로 지정가능.
System.out.print('*');
gap++;
}else{
System.out.print('\n');
gap=1;
}}
*/

/*위의 식 더 간단하게
System.out.println();
int gap=1;
int cell=4; //숫자 바꿔주는대로 정사각형달라짐
int limit=cell+1;
for(int i=0; i if(gap!=limit){
System.out.print('*');
gap++;
}else{
System.out.print('\n');
gap=1;
}}*/

 

 

System.out.println("\nQ2");

int[][] array = {{1,2,3},{4,5,6},{7,8,9}};{
for(int i=0; ifor(int j=0; j System.out.print(array[i][j] + " ");}
System.out.println();}}


/*다른접근
//s.o.p("1 2 3") //1+3*0 2+3*0 3+3*0
//s.o.p("4 5 6") //1+3*1 2+3*1 3+3*1
//s.o.p("7 8 9") //1+3*2 2+3*2 3+3*2

for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
System.out.println((j+1)+3*1+" ");
}
System.out.println();}

//또는 이방법
for(int i=1; i<10; i++){
System.out.print(i);
if(i%3==0){
System.out.println();
}else{
System.out.print(' ');
}}
*/

System.out.println("\nQ3");

for(int i3=1; i3<4; i3++){
for(int j3=0; j3<3; j3++){
System.out.print(i3+j3 + " ");}
System.out.println(" ");}


/*다른접근
0+1 1+1 2+1
1+1 2+1 3+1
2+1 3+1 4+1

for(int i=0; i<3; i++){
for(int j=1; j System.out.print(j+1);
}
System.out.println();}

//또다른방법
123(\n)234(\n)345


int newLine=4;//새로 바뀌는 줄
for(int i=1; i<6; i++){
if(i==newLine){
System.out.println();
newLine++;
i--;
i--;
i--; //두개 합치려면 i-=3; -이러면 두개뺀거랑 동일한 효과
}else{
System.out.print(i);
}}}
*/

 

 


System.out.println("\nQ4");

for(int i4=1; i4<5; i4++){ //(int i4=1; i4<3(이 수에따라 줄바뀜); i4++)

for(int j4=1; j4
System.out.print("★");}
System.out.println();}


System.out.println("\nQ5");

int su5=1;
for(int i5=1; i5<5; i5++){

for(int j5=1; j5
System.out.print(su5++);}
System.out.println();}

 

System.out.println("\nQ6");

for(int i6=0; i6<4; i6++){
for(int j6=0; j6 System.out.print(j6+1);}
System.out.println(" ");}

 

System.out.println("\nQ7");

int i7=0;
int j7=0;
for(i7=2; i7<9; i7++){
for(j7=1; j7<9; j7++){
System.out.print(i7 + "x" + j7 + "=" + i7*j7 + " ");}

System.out.println(i7 + "x" + j7 + "=" + i7*j7 + " ");}
}

}

/*풀이

for(int j=2; j<10; j++){
for(i=1; i<10; i++){ ---이렇게짜면 세로로만 나오고 원하는 출력안됨
---------------
*/
/*먼저 결과를 생각하고 코드짤것

System.out.print(i + "x" + j + "=" + i*j) //2단

System.out.println();

System.out.print(i+1 + "x" + j+1 + "=" + i+1*j+1) //3단
*/

//제대로된 풀이법 - 구구단이 세로로나오는 방법
for(int j7=1; j7<10; j7++){
for(int i7=2; i7<10; i7++){
System.out.print(i7+"x"+j7+"="+i7*j7+"\t");}
System.out.println();}

 

 

 

 

 

 

 

 

 

class Ex35{
int[] arr2=new int[0];

void add(int a){
int[] temp=new int[arr2.length+1]; //배열값 더 늘려야함
for(int i=0; i temp[i]=arr2[i];
}
arr2=temp;
arr2[arr2.length-1]=a;
}

 

public static void main(String[] args){
Ex35 me=new Ex35();
me.add(1);
me.add(10);
me.add(123);
me.add(6547);
for(int i=0; i System.out.println(arr1[i]);}}


/*int[] arr1;
arr1=new int[5];
for(int i=0; i System.out.prinlnt(arr1[i]);}
*/


}
}

728x90

'코딩 > 자바 기초특강' 카테고리의 다른 글

20.11.18 WED  (0) 2020.11.18
20.11.17 TUE 메소드  (0) 2020.11.17
20.11.05 THU 자바 기초특강  (0) 2020.11.05
20.11.04 WED 3일차 과제 --11.05 THU 과제 피드백  (0) 2020.11.04
20.11.04 WED 자바 기초특강 3  (0) 2020.11.04

댓글