代碼實現:給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。


import java.util.Scanner;
//給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = 0 ;
		String s = "0";
		System.out.println("請一個輸入整數:");
		while (true) {
			s = sc.nextLine();
			try {
				 num = Integer.parseInt(s);
				break;
			} catch (NumberFormatException e) {

				System.out.println("輸入非法,請重新輸入整數:");
			}
		}
		System.out.println(num+"整數的長度為:"+s.length());
		StringBuffer sb = new StringBuffer();
		sb.append(s);
		sb.reverse();
		System.out.println(sb.toString());		
	}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM