ch02ex.java


/*
* 1. 0이 입력될때까지 정수를 입력하고, 0이 입력되면 합/평균/최대/최소값을 구한다.
* 2. 4자리 정수 나르시스트 수를 출력하라.
*/
import java.io.*;
import java.util.Scanner;
import java.lang.Exception;

public class ch02ex {

public static void ChkNum(Integer n){
System.out.print("=+= "+ n +"숫자만 입력하세요.");
}

public static void main(String[] args){
int sum = 0, max = 0, min = 0, reader = 0, count = 0;

Scanner stdin = new Scanner(System.in);

try{
while(true){
System.out.print("정수를 입력하세요");
reader = stdin.nextInt();
ChkNum(reader);

if(reader == 0) break;
sum += reader;
count++;
if(reader>max){
max = reader;
}
if(min == 0 || min > reader){
min = reader;
}

}

System.out.printf("총합계는 %5d 이고, 평균은 %.2f , 최대값은 %5d 최소값은 %5d\n", sum,(float)sum/count, max,min);


//--- 4자리 정수 나르시스트 수 :두자리씩 구분하여 제곱하고 더한 수가 원래의 수와 같은 수
int first = 0, second = 0, hap = 0;
for(int i = 1000; i <= 9999; i++){
first = i/100;
second = i%100;
hap = (first*first)+(second*second);
if(hap == i){
System.out.println("네자리 정수 나르시스트수는 "+ i +" 입니다.");
}
}

//--- 3자리 정수 나르시스트수
int a=0, b=0, c=0;
System.out.println();
for(int i=100;i <= 1000;i++){
a=i/100;
b=i%100/10;
c=i%10;
hap = (a*a*a) + (b*b*b) + (c*c*c);
if(hap == i){
System.out.println("세자리 정수 나르시스트수는 "+ i +" 입니다.");
}
}

//--- 구구단 가로 출력
for(int i = 1; i< 10; i++){
for(int j = 1;j < 10; j++){
System.out.printf(" %4d * %d = %d ",j,i,i*j);
}
System.out.println();
}

//--- 알파벳 도형찍기
for(int i = 65; i <= 90;i++ ){
for(int j = 65;j <= i; j++){
System.out.print((char)j);
}
System.out.println();
}

//--- 각수까지의 합(for 문 2개 이용)
hap = 0;
for(int i = 1;i <=100;i++){
for(int j = 1;j <= i;j++){
hap += j;
}
}
System.out.print("\n 총 합은 "+ hap +" 입니다. ");

//--- 각수까지의 합(for 문 하나 이용)
hap = 0;
count = 0;
for(int i = 1;i <=100;i++){
hap += i;
count += hap;
}
System.out.print("\n 총 합은 "+ count +" 입니다. ");



}catch(NumberFormatException e){

System.out.println("숫자만 넣어줍니다.");

}catch(Exception e){

System.out.println("알수 없는 예외상황입니다. \n Exception Message :" + e.getMessage());
//e.printStackTrace();

}finally{

//--- 예외가 실행되든 안되든 무조선 실행한다.
System.out.println("\n 프로그램 종료입니다.");

}







}
}
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기

Posted by 홍반장

2009/10/06 18:17 2009/10/06 18:17
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/4688

Trackback URL : http://tcbs17.cafe24.com/tc/trackback/4688

« Previous : 1 : ... 1756 : 1757 : 1758 : 1759 : 1760 : 1761 : 1762 : 1763 : 1764 : ... 6391 : Next »

블로그 이미지

- 홍반장

Archives

Recent Trackbacks

Calendar

«   2024/05   »
      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  
Statistics Graph

Site Stats

Total hits:
196651
Today:
396
Yesterday:
673