1:给你一串数字,比如1234567890,变成1,234,567,890(字符串形式)
import java.util.Stack; public class Answer { public static void main(String[] args) { String a = "112348554"; Stack stck = new Stack(); for (int i = a.length()-1; i>=0; i--) { stck.add(a.charAt(i)); if(i % 3 == 0 && i >0) { stck.add(","); } } StringBuffer sb = new StringBuffer(); while(!stck.empty()) { sb.append(stck.pop()); } System.out.println(sb.toString()); } }
2:给你一串字符串数字,把他变成整型的数字,比如:‘1234123’ 转变为 1234123,不能使用工具类
public class Answer { public static void main(String[] args) { String a = "3322"; int record = 0; for (int i = 0; i < a.length(); i++) { char temp = a.charAt(i); int diff = temp - '0'; record = diff + record*10; } System.out.println(record); } }