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

20.11.04 WED 3일차 과제 --11.05 THU 과제 피드백

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

/*
1. 1~10까지 홀수를 출력하시오.
2. 1~10까지의 짝수를 출력하고 그 합을 구하시오
3. 다음을 출력하시오.
1+2+3+4+5+6+7+8+9+10=0000
4. 1-50중 4의 배수를 출력하시오.
5. 다음 의 합을 구하시오
1/2+2/3+3/4+4/5+....+9999/10000=?
*/

class que{

public static void main(String[] args){

//1번문제
System.out.println("1. 1-10까지 홀수 출력");
for(int a=1; a<=10;a++){
System.out.print(a++ + " ");}

//2번문제
System.out.println("\n\n2. 1-10까지 짝수를 출력하고 그 합을 구하시오");

int total1 = 0;
for(int b=1; b<=10;b++){
if(b%2==0){
System.out.print(b + " ");
total1+=b;}}
System.out.println("\n총 합 : " + total1);

//더하는값


//3번문제
System.out.println("\n3. 1+2+3+4+5+6+7+8+9+10=0000 출력하시오");

int total2 = 0;
for(int c=1; c<=10; c++){
System.out.print(c + "+");
total2+=c;}
System.out.println("="+ total2); //마지막에 +를 빼는방법...?


//4번문제
System.out.println("\n4. 1-50중 4의 배수를 출력하시오.");


for(int d=1; d<=50; d++){
if(d%4==0){
System.out.print(d+" ");}}

 


//5번문제
System.out.println("\n\n5. 다음의 합을 구하시오\t1/2+2/3+3/4+4/5+....+9999/10000=?");

double total5 = 0;
for(double e=1; e<=9999; e++){
total5+=(e/(e+1));}
System.out.println(total5);
}
}

 


 

피드백

1. 1~10까지 홀수를 출력하시오.

System.out.println(a);
a+=2; //a에2를 더해주는 증감연산자!! //잘 모르겠으면 이 식을 먼저 생각해 줄 것


for(int a=1; a<=10;a++){ //a++대신에 a+=2 도 가능 - 그럼 밑에 a만 출력해야함
System.out.print(a++ + " ");}

for(int a=1; a<=10;a++){ //a++대신에 a+=2 도 가능 - 그럼 밑에 a만 출력해야함
            if(a%2==1){System.out.print(a);}


for(int a=1; a<=10;a++){ //a++대신에 a+=2 도 가능 - 그럼 밑에 a만 출력해야함
           if(a%2!=0){System.out.print(a);} //else값도 넣어주고 " "로 출력하면됨

int a=1;
while(a<+10){
if(a%2!=0){System.out.print(a);}
else{System.out.print(" ");}
a++;

 

2. 1~10까지의 짝수를 출력하고 그 합을 구하시오

int total1 = 0;
for(int b=1; b<=10;b++){
if(b%2==0){
System.out.print(b + " ");
total1+=b;}}
System.out.println("\n총 합 : " + total1);

 

3. 다음을 출력하시오.
1+2+3+4+5+6+7+8+9+10=0000

int total2 = 0;
for(int c=1; c<=10; c++){
System.out.print(c + "+");
total2+=c;}
System.out.println("="+ total2); //마지막에 +를 빼는방법...?

int total2 = 0;
for(int c=1; c<=10; c++){
System.out.print(c);
total2 += c;
if(c!=10){
System.out.print('+');}
else{System.out.print('=');}
System.out.print(total2);} //오류해결 - total2값이 for문안에서 반복되고 있었음..... }에 ★주석 달기!!!!

int total2 = 0;
for(int c=1; c<=10; c++){
if(c!=1){
System.out.print('+');}
System.out.print(c);} // 여기에 } 붙여서 오류났던것...
total += c;}
System.out.print("="+total2);   //왜오류나지.....?-해결

 


+보너스 문제 : 50까지 출력하고싶을때


int total2=0;

int limit2=50;


for(int c=1; c<=limit2; c++){
밑에는 동일

 

4. 1-50중 4의 배수를 출력하시오.


for(int d=1; d<=50; d++){
if(d%4==0){
System.out.print(d+" ");}}

 

보너스 문제
int d=1;
boolean k=true;
while(k){
        System.out.println(4*d);
        d++;
        if(4*d>50){k=false;}

boolean k=true;
for(int m=1; k; m++){
    System.out.println(m*4);
    if((m+1)*4>50){k=false;}

 

 

5. 다음의 합을 구하시오 1/2+2/3+3/4+4/5+....+9999/10000=?

double total5 = 0;
for(double e=1; e<=9999; e++){
total5+=(e/(e+1));}
System.out.println(total5);
}
}

 

int sum4=0;   //1-1/2  +  1-1/3  +  1-1/4 +  .....
for(int n=1; n<10000; n++){
sum4+=1.0-1.0/(n+1); //1.0으로 써야함
}
System.out.println(sum
}}4);

 

 

 

 

 

 

 

728x90

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

20.11.06 FRI 자바기초특강 5  (0) 2020.11.06
20.11.05 THU 자바 기초특강  (0) 2020.11.05
20.11.04 WED 자바 기초특강 3  (0) 2020.11.04
20.11.03 TUE 자바 기초특강 2  (0) 2020.11.03
20.11.02 Mon 자바 기초특강 1  (0) 2020.11.03

댓글