// 두개의 정수를 입력받아서 합게를 산출하는 프로그램
import java.io.*;
class AddInteger
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
String string1, string2; // 객체참조변수 (포인터와 같은 개념)
int num1, num2, sum; // 기본자료형
System.out.print ("Enter the first number : ");
string1 = stdin.readLine();
num1 = Integer.parseInt (string1); // 문자열을 인트형으로 해석해주는 메소드 (C의 atoi와 동일.)
System.out.print ("Enter the second number : ");
string2 = stdin.readLine();
num2 = Integer.parseInt (string2);
sum = num1 + num2;
System.out.print ("The sum of 2 number is [" + sum + "]");
}
}
Integer.parseInt (string1); 이부분이 문자열을 인트형으로 해석해주는 부분이다.import java.io.*;
class AddInteger
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
String string1, string2; // 객체참조변수 (포인터와 같은 개념)
int num1, num2, sum; // 기본자료형
System.out.print ("Enter the first number : ");
string1 = stdin.readLine();
num1 = Integer.parseInt (string1); // 문자열을 인트형으로 해석해주는 메소드 (C의 atoi와 동일.)
System.out.print ("Enter the second number : ");
string2 = stdin.readLine();
num2 = Integer.parseInt (string2);
sum = num1 + num2;
System.out.print ("The sum of 2 number is [" + sum + "]");
}
}
C언어의 atoi함수와 동일하다.
'Java > 강의' 카테고리의 다른 글
인수 전달 기법 (0) | 2011.02.09 |
---|---|
생성자(Class Constructors) (0) | 2011.02.08 |
키보드 입력받기!! (C의 scanf) (0) | 2011.02.07 |
print 와 println 의 차이점! (0) | 2011.02.07 |
"HelloWorld" 화면에 출력하기 (0) | 2011.01.31 |