1.數字轉換字符串

將int類型數字轉換成字符串

 ①將數字從個位開始依次取出--取餘

 ②得到的字符串是逆序的,需要翻轉處理

public class NumToStr {
    public static void main(String[] args) {
		int num = 1234;
		String str = "";
		String newstr = "";
		while (num!=0){
			
			str+= (num%10);//""+4+3+2+1=4321
			num /= 10;
		}
		//字符串str爲4321,需要逆序處理
		for (int i = str.length()-1; i>=0; i--){
			newstr += str.charAt(i);
		}
		System.out.println(newstr);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章