java #5 BinaryString/Arguments/parseInt/제어문
1 2 3 4 5 6 | class BinaryString{ public static void main(String[] args) { int i=1531315153; System.out.println(i+"를 이진수로 "+Integer.toBinaryString(i)); } } | cs |
toBinaryString
public static String toBinaryString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 2.The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading
0
s. If the unsigned magnitude is zero, it is represented by a single zero character'0'
('\u0030'
); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters'0'
('\u0030'
) and'1'
('\u0031'
) are used as binary digits.- Parameters:
i
- an integer to be converted to a string.- Returns:
- the string representation of the unsigned integer value represented by the argument in binary (base 2).
- Since:
- JDK1.0.2
1 2 3 4 5 | c:\> java T 10 안녕 3.5 <- java를 실행할 경우 String[] args[0] : 10 String[] args[1] : 안녕 String[] args[2] : 3.5 | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* main method에 외부값 (aguments) 를 넣어서 사용하는 클래스 실행법 : java Arguments 값 값 값 .... */ class Arguments { public static void main(String[] args) { System.out.println( args[0] ); System.out.println( args[1] ); System.out.println( args[2] ); System.out.println( args[3] ); System.out.println( args[0] + args[1] ); /// 문자열을 정수로 변환 int num1=Integer.parseInt(args[0]); int num2=Integer.parseInt(args[1]); System.out.println( num1 +"+"+ num2+"="+(num1+num2)); } } | cs |
- String에 저장된 문자열 형태의 숫자를 int형으로 변환하는 Integer클래스의 메소드이다.
- 아래는 java api(http://docs.oracle.com/javase/7/docs/api/index.html)에서 발췌
public static int parseInt(String s) throws NumberFormatException
'-'
('\u002D'
) to indicate a negative value or an ASCII plus sign '+'
('\u002B'
) to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int)
method.- Parameters:
s
- aString
containing theint
representation to be parsed- Returns:
- the integer value represented by the argument in decimal.
- Throws:
NumberFormatException
- if the string does not contain a parsable integer
* 제어문
조건문 | 조건에 따라서 코드를 수행할때 if, else, switch~case | ||||||||||||
단일if | 조건에 맞을때에만 코드 수행 | ||||||||||||
if~else | 둘중 하나를 실행해야 할 때
| ||||||||||||
다중if(else~if) | 여러개의 조건을 비교해서 코드를 실행
| ||||||||||||
switch~case | - 정수만 비교할 때 사용하는 조건문 - jdk1.7부터 문자열까지 비교할 수 있다. - 분기문인 break; 와 같이 사용할 수 있다. switch, for, while, do~while을 빠져나감. - case의 상수는 인접한 수를 쓰거나 규칙이 있는 상수를 사용하는 것이 좀더 빠른 비교를 하게 된다.
#1
#2
| ||||||||||||
반복문 | 조건에 맞을때 까지 코드를 반복실행할 때 - 반복문을 빠져나가는 분기문은 break; - 반복문의 수행을 건너뛸 때에는 continue; - 조건을 잘못 부여하면 코드를 계속 수행하는 무한루프에 빠진다.(프로그램이 종료되지 않는다.) (강제정지/인터럽트.도스창=컨트롤+c) - for, while, do~while | ||||||||||||
for | 시작과 끝을 알 때 사용하는 반복문 ※ 반복문 안에서 변수를 선언하지 않는다. *무한loop 종료되지 말아야할 프로그램 작성할
이때, 무한루프 for문 아래의 코드는 죽은코드가 되어 에러 발생.
| ||||||||||||
while | - 시작과 끝을 모를 때 사용 - 한번도 실행이 안될수 있고 최대 조건까지 수행
| ||||||||||||
do~while | - 시작과 끝을 모를 때 사용 - 최소 한번 수행
| ||||||||||||
분기문 | break, continue, return |
'컴퓨터 관련 > JAVA 강의' 카테고리의 다른 글
java #7 문자열 비교(equals), Arguments (0) | 2016.12.18 |
---|---|
java #6 패키지 (0) | 2016.12.18 |
java #3, #4 Constant/형변환/API/연산자/진수 (0) | 2016.12.18 |
java #2 java 기초2 (0) | 2016.12.18 |
java #1 java 기초1 (0) | 2016.12.18 |