用栈实现钱币显示问题,逗号分隔三位
3条回答 默认 最新
- shawn.bug 2009-12-02 12:51关注
package math;
import java.util.Stack;
public class Test {
public static void main(String args[]){
long i=1234567;
System.out.println(strFormat(i));
long i1=1234567;
System.out.println(strFormat(i1));
long i2=1234567;
System.out.println(strFormat(i2));} /* * 处理字串型 */ public static String strFormat(String str){ Stack<Character> sta =new Stack<Character>(); StringBuffer sb=new StringBuffer(); char[] charstr=str.toCharArray(); for(char a:charstr){ sta.push(a); } Stack<String> type= new Stack<String>(); int i=0; while(!sta.empty()){ if(i%4==0){ type.push(","); } else { type.push(sta.pop().toString()); } i++; } while(!type.empty()){ String s=type.pop(); sb.append(s); } String fs=sb.toString().substring(0,sb.toString().lastIndexOf(",")); return fs; } /* * 处理长整形 */ public static String strFormat(Long l){ String str=String.valueOf(l); Stack<Character> sta =new Stack<Character>(); StringBuffer sb=new StringBuffer(); char[] charstr=str.toCharArray(); for(char a:charstr){ sta.push(a); } Stack<String> type= new Stack<String>(); int i=0; while(!sta.empty()){ if(i%4==0){ type.push(","); } else { type.push(sta.pop().toString()); } i++; } while(!type.empty()){ String s=type.pop(); sb.append(s); } String fs=sb.toString().substring(0,sb.toString().lastIndexOf(",")); return fs; } /* * 处理整形 */ public static String strFormat(int l){ String str=String.valueOf(l); Stack<Character> sta =new Stack<Character>(); StringBuffer sb=new StringBuffer(); char[] charstr=str.toCharArray(); for(char a:charstr){ sta.push(a); } Stack<String> type= new Stack<String>(); int i=0; while(!sta.empty()){ if(i%4==0){ type.push(","); } else { type.push(sta.pop().toString()); } i++; } while(!type.empty()){ String s=type.pop(); sb.append(s); } String fs=sb.toString().substring(0,sb.toString().lastIndexOf(",")); return fs; }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 win10权限管理,限制普通用户使用删除功能
- ¥15 minnio内存占用过大,内存没被回收(Windows环境)
- ¥65 抖音咸鱼付款链接转码支付宝
- ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
- ¥15 求螺旋焊缝的图像处理
- ¥15 blast算法(相关搜索:数据库)
- ¥15 请问有人会紧聚焦相关的matlab知识嘛?
- ¥15 网络通信安全解决方案
- ¥50 yalmip+Gurobi
- ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面