JAVA

3일차

구자룡 2021. 3. 8. 17:02

 

public class EXAM01 {

    public static void main (String[] ar) {

       for(int i=100;i<=500;i=i+100) {

           System.out.println("*");

          

       }

    }

}

 

결과:

*
*
*
*
*


public class Exam02 {

    public static void main(String[] ar) {

       for(int i=1;i<=5;++i) {

       System.out.println(i);

    }

    System.out.println();

   

    for(int i=5;i>=1;i--) {

           System.out.println(i);

 

}

}

}

결과:

5

5
4
3
2
1


public class Exam03 {

    public static void main(String[] ar) {

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

//         if(i==2) {

//            break;

//         }

           if(i==2) {

              continue;

           }

           System.out.println(i);

          

       }

    }

 

}

결과:

0
1
3
4


public class Exam04 {

    public static void main(String[] ar) {

       int tot=0,tot2=0;

       for(int i=1;i<=100;++i) {

           tot2 +=i; //tot2=tot2+i;

           }//100가지의

   

       for(int i=1;i<=100;++i) {

           if(i%2==0) {

           tot +=i; //tot=tot+i;

           }//100가지의 짝수의합

       }

       System.out.print("100까지의 tot2="+tot2);

       System.out.print("짝수의 tot="+tot);

}

}

결과:100까지의 합 tot2=5050짝수의 합 tot=2550


//다중for(중첩for)

public class Exam05 {

    public static void main(String[] ar) {

    for(int i=1;i<=5;++i) {

       for(int a=1;a<=5;++a) {

           System.out.print("*");//*****

       }

       System.out.println();

    }

    }

 

}

결과:

*****
*****
*****
*****
*****


//for문을 이용하여 알파벳을 출력시켜라

public class Exam06 {

    public static void main(String[] ar) {

//     for(int i=65;i<=90;++i) {

//         System.out.print((char)i);

//     }

       for(char i='A';i<='Z';++i) {

           System.out.print(i+":");

           for(char c=i;c<='Z';++c) {

              System.out.print(c);

           }

           System.out.println();

       }

    }

 

}

결과:

A:ABCDEFGHIJKLMNOPQRSTUVWXYZ
B:BCDEFGHIJKLMNOPQRSTUVWXYZ
C:CDEFGHIJKLMNOPQRSTUVWXYZ
D:DEFGHIJKLMNOPQRSTUVWXYZ
E:EFGHIJKLMNOPQRSTUVWXYZ
F:FGHIJKLMNOPQRSTUVWXYZ
G:GHIJKLMNOPQRSTUVWXYZ
H:HIJKLMNOPQRSTUVWXYZ
I:IJKLMNOPQRSTUVWXYZ
J:JKLMNOPQRSTUVWXYZ
K:KLMNOPQRSTUVWXYZ
L:LMNOPQRSTUVWXYZ
M:MNOPQRSTUVWXYZ
N:NOPQRSTUVWXYZ
O:OPQRSTUVWXYZ
P:PQRSTUVWXYZ
Q:QRSTUVWXYZ
R:RSTUVWXYZ
S:STUVWXYZ
T:TUVWXYZ
U:UVWXYZ
V:VWXYZ
W:WXYZ
X:XYZ
Y:YZ
Z:Z


public class Exam07 {

    public static void main(String [] ar) {

       aaa:for(int j=1;j<=5;++j) {

           bbb:for(int i=1;i<=5;++i) {

           System.out.print("*");

           if(i==2) continue aaa;

       }

       System.out.println();

 

}

}

}

결과:**********


public class Exam08 {

    public static void main(String [] ar) {

       int i=1,tot=0;

       while(i<=10) {

           tot += i;//tot=tot+i

           ++i;

          

       }

       System.out.println("tot"+tot);

    }

 

}

결과:tot55


import java.io.*;

public class Exam09 {

    public static void main(String[] ar) throws IOException{

       while(true) {

           System.out.print("문자=");

           char ch=(char)System.in.read();

           System.in.read();//엔터키의 \r(41?) \n(14?)

           System.in.read();

           System.out.println("ch="+ch);

       }

    }

 

}

결과:

문자=A
ch=A
문자=a
ch=a
문자=


import java.io.*;

public class Exam09 {

    public static void main(String[] ar) throws IOException{

       while(true) {

           System.out.print("문자=");

           char ch=(char)System.in.read();

           System.in.read();//엔터키의 \r(41?) \n(14?)

           System.in.read();

           System.out.println("ch="+ch);

          

           System.out.println("계속(y/n)");

           char c=(char)System.in.read();

           System.in.read();

           System.in.read();

           if(c=='N'||c=='n') break;

       }

    }

 

}

결과:

문자=a
ch=a
계속(y/n)
y
문자=c
ch=c
계속(y/n)
n


//do while

import java.io.*;

public class Exam10 {

    public static void main(String [] ar) throws IOException {

       BufferedReader in=

              new BufferedReader(new InputStreamReader(System.in));

       int kor=0,mat=0,eng=0,tot=0;

       float avg=0.0f;

       do {

       System.out.print("kor=");

       kor=Integer.parseInt(in.readLine());

       }while(kor<0 || kor>100);//0보다 적거나 100보다 크다면 다시 실행하라는 뜻임.

      

       do {

       System.out.print("mat=");

       mat=Integer.parseInt(in.readLine());

       }while(mat<0 || mat>100);//0보다 적거나 100보다 크다면 다시 실행하라는 뜻임.

      

       do {

       System.out.print("eng=");

       eng=Integer.parseInt(in.readLine());

       }while(eng<0 || eng>100);//0보다 적거나 100보다 크다면 다시 실행하라는 뜻임.

      

       tot=kor+mat+eng;

       avg=tot/3.0f;

       System.out.println("총점수="+tot);

       System.out.println();

       System.out.println("평균="+avg);

       System.out.printf("avg=%.2f", avg);

      

    }

 

}

결과:

kor=1000
kor=100
mat=-49
mat=40
eng=77
총점수=217

평균=72.333336
avg=72.33점


import java.io.InputStreamReader;

public class Exam11 {

   public static void main(String[] ar) throws IOException{

       BufferedReader in=//key 기능객체가 되어버린내용.

             new BufferedReader(new InputStreamReader(System.in));

//      int x=0,y=0,z=0;

       int[]x=new int[3];//초기값의 자동화

       //beelean --->false

       //byte,shart,int -->0

       //char -->0,'\0'

       //float --->0.0f

       System.out.print("x=");

       x[0]=Integer.parseInt(in.readLine());

       System.out.print("y=");

       x[1]=Integer.parseInt(in.readLine());

       System.out.print("z=");

       x[2]=Integer.parseInt(in.readLine());

      

       System.out.println();

       System.out.println("x="+x[0]);

       System.out.println("y="+x[1]);

       System.out.println("z="+x[2]);

   }

 

}

결과:

x=10
y=20
z=20

x=10
y=20
z=20


public class Exam12 {

   public static void main(String[] ar) {

       int kor=0,eng=0,mat=0;

       int[] sub;

       sub=new int[3];//디폴트초기화가 .

      

       sub=new int[] {100,70,50};

       System.out.println("kor="+sub[0]);

       System.out.println("eng="+sub[1]);

       System.out.println("mat="+sub[2]);

   }

 

}

결과:

kor=100
eng=70
mat=50


public class Exam13 {

   public static void main(String[] ar) {

       int [] x=new int[5];

       boolean [] y=new boolean[3];

      

       for(int i=0;i<x.length;++i) {

          System.out.println("x["+i+"]="+x[i]);

       }

//     System.out.println("x[0]="+x[0]);

//     System.out.println("x[0]="+x[1]);

//     System.out.println("x[0]="+x[2]);

//     System.out.println("x[0]="+x[3]);

//     System.out.println("x[0]="+x[4]);

      

       for(int i=0;i<y.length;++i) {

          System.out.println("y["+i+"]="+y[i]);

       }

//     System.out.println("y[0]="+y[0]);

//     System.out.println("y[0]="+y[1]);

//     System.out.println("y[0]="+y[2]);

   }

 

}

결과:

x[0]=0
x[1]=0
x[2]=0
x[3]=0
x[4]=0
y[0]=false
y[1]=false
y[2]=false


//외부에서 배열사이즈를 입력받아 진행하는 프로그램을 작성하라

import java.io.*;

 

public class Exam14 {

   public static void main(String[] ar) throws IOException {

       BufferedReader in=

             new BufferedReader (new InputStreamReader(System.in));

       int num=0;

       int[] x=null;

       System.out.print("num=");

       num=Integer.parseInt(in.readLine());

      

       x=new int[num];//int x[]= new int x[num];

       for(int i=0;i<x.length;++i) {

          System.out.println("x[+i+]="+x[i]);

       }

      

   }

 

}

 결과:

num=7
x[+i+]=0
x[+i+]=0
x[+i+]=0
x[+i+]=0
x[+i+]=0
x[+i+]=0
x[+i+]=0


//사람의 이름과 나이를 입력받아 출력하는 프로그램을 작성하라

import java.io.*;

public class Exam15 {

   public static void main(String[] ar) throws IOException{

       BufferedReader in=

             new BufferedReader(new InputStreamReader(System.in));

       int inwon = 0;

       String[] name=null;

       int[]age=null;

       System.out.print("인원=");

      inwon=Integer.parseInt(in.readLine());

       name=new String[inwon];

       age=new int[inwon];

      

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

          System.out.print(i+1+"번째사람이름=");

          name[i]=in.readLine();

          System.out.print(i+1+"번째 나이=");

          age[1]=Integer.parseInt(in.readLine());

       }

       System.out.println();

      

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

   System.out.println(i+1+":"+name[i]+"\t"+":"+age[1]);

   }

  

   }

}

결과:

인원=3
1번째사람이름=aaa
1번째 나이=25
2번째사람이름=a
2번째 나이=24
3번째사람이름=dd
3번째 나이=20

1:aaa :20
2:a :20
3:dd :20


//입력인원을 산정하여 각각의 인원의 국어 영어 수학점수를 입력하고 각각의 총점과

//평균등을 출력시키고 등수를 또한 출력하는 프로그램을 작성하라.

import java.io.*;

public class Exam16 {

    public static void main(String[] ar) throws IOException{

       BufferedReader in=

              new BufferedReader(new InputStreamReader(System.in));

        //ar매개변수를 체크해서 진행을 한다면...

       if(ar.length ==0) {

           System.out.println("입력이없음.");

           System.exit(0);//정상종료..(1,-1):비정상종료

       }

      

        System.out.println("ar.length="+ar.length);

    }

 

}

결과:

ar.length=3


//입력인원을 산정하여 각각의 인원의 국어 영어 수학점수를 입력하고 각각의

// 총점과 평균등수를 출력시키고 등수를 또한 출력하는 프로그램을 작성하라

import java. io.*;

public class Exam16 {

   public static void main(String []ar) throws IOException {

      BufferedReader in =

            new BufferedReader(new InputStreamReader(System.in));

     

     

      System.out.print("인원수는?");

      int inwon=Integer.parseInt(in.readLine());

      String [] name=new String[inwon];

      int kor[]=new int[inwon];

      int eng[]=new int[inwon];

      int [] math=new int[inwon];

      int [] tot=new int[inwon];

      float avg[]=new float[inwon];

      int[] rank=new int[inwon];

     

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

         System.out.print(i+"번째사람이름=");

         name[i]=in.readLine();

         System.out.print(i+"한국어점수=");

         kor[i]=Integer.parseInt(in.readLine());

         System.out.print(i+1+"영어점수");

         eng[i]=Integer.parseInt(in.readLine());

         System.out.print(i+1+"수학점수");

         math[i]=Integer.parseInt(in.readLine());

        

         tot[i]=kor[i]+eng[i]+math[i];

         avg[i]=tot[i]/3.0f;

              

         rank[i]=1;

      }

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

            for(int j=0;j<inwon;++j) {

               if(tot[i]<tot[j]) rank[i]++;

         }

         }

         System.out.println();

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

            System.out.print(name[i]+"\t");

            System.out.print(kor[i]+"\t");

            System.out.print(eng[i]+"\t");

            System.out.print(math[i]+"\t");

            System.out.print(tot[i]+"\t");

            System.out.printf("%.2f\t",avg[i]);

            System.out.print(rank[i]);

           

            }

         }

        

        

        

  }

결과:

인원수는?1
0번째사람이름=aaa
0한국어점수=90
1영어점수90
1수학점수90

aaa 90 90 90 270 90.00 1

'JAVA' 카테고리의 다른 글

6일차  (0) 2021.03.11
5일차  (0) 2021.03.10
4일차  (0) 2021.03.09
2일차  (0) 2021.03.05
1일차  (0) 2021.03.05