[Day10] Java 10 [8/22]
1. String ํด๋์ค ํจ์(๋ฉ์๋)
charAt(index)
equals()
split(regex)
matches(regex)
substring()
trim()
valueOf()
format()
join()
2. 10์ง์ 2์ง์ ์ ํ
// [๋ฌธ์ ] 10์ง์ ์ ์๋ฅผ ์
๋ ฅ๋ฐ์์
// 2๋ฐ์ดํธ 2์ง์ํํ๋ก ์ถ๋ ฅํ์ธ์.
// ์) 10
// 0000 0000 0000 1010
Scanner scanner = new Scanner(System.in);
System.out.print("> 10์ง์ ์ ์ ์
๋ ฅ ? ");
int n = scanner.nextInt();
int [] binaryArray = new int[16];
int index = binaryArray.length - 1;
// 4:10 ํ์ด ์์
// n = 10
int share = n, rest ;
/*
share = share /2; // 5
rest = share % 2; // 0
binaryArray[ index ] = rest;
index--;
share = share /2; // 2
rest = share % 2; // 1
binaryArray[ index ] = rest;
index--;
share = share /2; // 1
rest = share % 2; // 0
binaryArray[ index ] = rest;
index--;
:
:
*/
// ์ค๋จ์ (break point) F11 ๋๋ฒ๊น
~
while( share != 0 ) {
rest = share % 2;
share = share /2;
binaryArray[ index-- ] = rest;
}
// int [] ๋ฐฐ์ด์ ์ด๊ธฐํ ํ์ง ์์ผ๋ฉด int์ ๊ธฐ๋ณธ๊ฐ์ธ 0์ผ๋ก ์ด๊ธฐํ ๋์ด์ ธ ์๋ค...( ์๊ธฐ )
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println( Arrays.toString( binaryArray ) );
// 00000000000010
String result = Arrays.toString( binaryArray ) ;
result = result.replaceAll(",\\s", "");
System.out.println( result.substring(1, result.length()-1) );
String binaryN = Integer.toBinaryString( n )
'๐จโ๐ป Web Development > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day12] Java 12 - ๋ฐฐ์ด์ ํ์ฉ (0) | 2022.09.01 |
---|---|
[Day11] Java 11 - ๋ฌ๋ ฅ์ถ๋ ฅ, ๋ฐฐ์ด, N์ฐจ์ ๋ฐฐ์ด (0) | 2022.09.01 |
[Day9] Java 9 - ๋ก๋, ํฉํ ๋ฆฌ์ผ, ์นด๋๋ฒํธ (0) | 2022.09.01 |
[Day8] Java 8 - ์ ๊ทํํ์(regex), ๋ฉ์๋, ์ค๋ฒ๋ก๋ฉ, swap (0) | 2022.09.01 |
[Day7] Java 7 - ๋ฌ๋ ฅ, ๊ตฌ๊ตฌ๋จ (0) | 2022.08.29 |
์ต๊ทผ๋๊ธ