|NO.Z.00077|——————————|BigDataEnd|——|Java&循环结构.V16|——|Java.v16|while循环.v05|实现反向输出|

一、while循环实现反向输出
### --- 案例题目

~~~     ——>    提示用户输入一个任意位数的正整数然后反向输出
二、编程代码
### --- 编程代码

/*
    编程使用while循环实现任意正整数的反向输出
 */
import java.util.Scanner; 
 
public class WhileReverseTest {
    
    public static void main(String[] args) {
        
        // 1.提示用户输入一个正整数并使用变量记录  123
        System.out.println("请输入一个正整数:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        
        // 2.使用while循环进行拆分并打印
        //while(num > 0) {
            //System.out.print(num % 10);  // 拆分个位数
            //num /= 10;  // 丢弃个位数
        //}
        // 2.使用while循环拆分整数中的每个数字并记录到变量中
        int res = 0;
        int temp = num;  // 指定变量作为num的替身
        while(temp > 0) {
            res = res*10 + temp % 10; // 3     32   321
            temp /= 10;               // 12    1    0
        }
        
        // 3.打印逆序后的结果
        System.out.println(num + "逆序后的结果是:" + res);
    }
}
三、编译打印
### --- 编译

C:\Users\Administrator\Desktop\project>javac WhileReverseTest.java
### --- 打印输出

C:\Users\Administrator\Desktop\project>java WhileReverseTest
请输入一个正整数:
12345
12345逆序后的结果是:54321

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示