日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

键盘录入一个正整数,把它的各个位上的数字倒着排列形成一个新的整数并输出。 例如:12345 数出54321 78760 输出6787(0省去)

發布時間:2023/12/3 49 豆豆
package com.coffn.demos; /*** 4、鍵盤錄入一個正整數,把它的各個位上的數字倒著排列形成一個新的整數并輸出。例如:12345 數出54321 78760 輸出6787(0省去)*/ import java.util.Scanner;public class Demo1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個正整數:");int num = sc.nextInt();String str = num+"";//反轉String reverse =reverse(str);//統計反轉后0的個數int count = getSum(reverse);System.out.println();System.out.println("反轉后的數字為:"+getString(reverse,count));}//反轉public static String reverse(String str) {String result = "";for (int i = str.length()-1; i >= 0; i--) {result += str.charAt(i);}return result;}//統計反轉之后開頭0的個數public static int getSum(String str) {int count = 0;for (int i = 0; i < str.length(); i++) {char ch = str.charAt(i);if (ch == '0') {count++;}else {break;}}return count;}//消0public static String getString(String str, int count ) {//用于后面字符串的拼接String result = "";for (int i = count; i < str.length(); i++) {result += str.charAt(i);}return result;} }

總結

以上是生活随笔為你收集整理的键盘录入一个正整数,把它的各个位上的数字倒着排列形成一个新的整数并输出。 例如:12345 数出54321 78760 输出6787(0省去)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。