文字列と整数の交換
JAvaのapiは文字列と整型の交換を提供したが、acmの子供靴を作るには、ハードコードは必ず通るべきで、実は本人もojでいくつかの水問題をしただけで、acmをしたことがなく、劉汝佳の白書を読んで、虐げられているような気がして、私の普通の人をして、acmをするのは普通の人ではなく、普通は人ではない.
入力:
「3838438」(文字列)
5201314(整数)
出力:
3838438(整数)
「5201314」(文字列)
入力:
「3838438」(文字列)
5201314(整数)
出力:
3838438(整数)
「5201314」(文字列)
class HardCoded {
public static void main(String[] args) {
String str1 = "3838438";
int number1 = stringToInt(str1);
System.out.println(number1);
int number2 = 5201314;
String str2 = intToString(number2);
System.out.println(str2);
}
//
public static String intToString(int number) {
String str = "";
while(number != 0) {
int temp = number % 10;
number /= 10;
str = (char)(temp + '0') + str;
}
return str;
}
//
public static int stringToInt(String str) {
int result = 0;
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int number = c - '0';
result = result * 10 + number;
}
return result;
}
}