'예제프로그램'에 해당되는 글 1건

  1. 2007.07.16 초간단 프로그램 예제 두가지

1. 하나의 문자를 입력받아 그것의 Char 형 문자와 아스키 코드값을 표시하는 예제.
import java.io.*;

class Change {
    public static void main(String[] args) throws IOException{
       System.out.print("Type any Characters then Enter : ");
      
       int ch = System.in.read()         // 아스키 값을 입력받아, 아스키 값을 저장

       System.out.println( (char)ch + " = " + ch);         
            // 처음 ch 는 아스키 값의 해당되는 문자를 출력하기 위한 형변환
            // 두번쨰 ch 는 아스키 값을 그대로 출력하기 위한 것
    }
}

2. 한사람의 이름을 입력받고 국어, 영어, 수학 점수를 입력받아 총점과 평균을 구하는 프로그램.
import java.io.*;

class TotalScore{
    public static void main(String[] args) throws IOException {

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

       String name, kors, engs, maths = "";         // String 형의 초기화
       int kor, eng, math, total = 0;
       float avg = 0.0f;

       System.out.println("Input Your Information");
       System.out.print("What is Your Name? ");
       name = in.readLine();          // 이름의 문자열을 입력받아 저장
       System.out.print("What is Your Korean Score? ");
       kors = in.readLine();          // 숫자를 입력받지만, 실제로는, 숫자로된 문자열로 인식
       kor = Integer.parseInt(kors);          // Wrapper 클래스로, 숫자로된 문자열을 숫자로 변환
       System.out.print("What is Your English Score? ");
       engs = in.readLine();
       eng = Integer.parseInt(engs);
       System.out.print("What is Your Math Score? ");
       maths = in.readLine();
       math = Integer.parseInt(maths);

       total = kor + eng + math;
       avg = (float) total / 3;         // float 형으로 형변환

       System.out.println("");         // 일부로 한줄 내려주기 위해 사용
       System.out.println(name + "님의 성적은 다음과 같습니다.");
       System.out.println("총점 : " + total + "점");
       System.out.println("평균 : " + avg + "점");
    }
}
위의 소스코드를 다음과 같이 수정할 수 있다.


import java.io.*;

class TotalScore{
    public static void main(String[] args) throws IOException {

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

       String name = "";         // String 형의 초기화 ----------------------- 수정 ①
       int kor, eng, math, total = 0;
       float avg = 0.0f;

       System.out.println("Input Your Information");
       System.out.print("What is Your Name? ");
       name = in.readLine();          // 이름의 문자열을 입력받아 저장
       System.out.print("What is Your Korean Score? ");
       kor = Integer.parseInt(in.readLine());          // Wrapper 클래스로, 숫자로된 문자열을 숫자로 변환 ------------------------------------- 수정 ②, 이런식의 방법이 가능
       System.out.print("What is Your English Score? ");
       eng = Integer.parseInt(in.readLine()); // ------------------------- 수정 ③
       System.out.print("What is Your Math Score? ");
       math = Integer.parseInt(in.readLine()); // ------------------------ 수정 ④

       total = kor + eng + math;
       avg = (float) total / 3;         // float 형으로 형변환

       System.out.println("");         // 일부로 한줄 내려주기 위해 사용
       System.out.println(name + "님의 성적은 다음과 같습니다.");
       System.out.println("총점 : " + total + "점");
       System.out.println("평균 : " + avg + "점");
    }
}
:

BLOG main image
아무거나 공부하자!!! by Young79

공지사항

카테고리

분류 전체보기 (79)
Programing (29)
English (31)
Graphic (4)
Saying on T"We"tter (15)

최근에 올라온 글

최근에 달린 댓글

글 보관함

달력

«   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
Total :
Today : Yesterday :