首页 > 其他分享 >将输入的数字反向输出

将输入的数字反向输出

时间:2023-03-13 16:02:09浏览次数:26  
标签:输出 scanner opposite StringBuilder System num 反向 输入 out

package com.example.practice;

import java.util.Scanner;

//将输入的数字反向输出
public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一串数字:");
        if (scanner.hasNextInt()) {
            int num = scanner.nextInt();
            StringBuilder builder = new StringBuilder();
            StringBuilder opposite = null;
            while (num != 0) {  //"%"表示取余数
                int res = num % 10; //取出最后一位
                num = num / 10;
                opposite = builder.append(res);
            }
            System.out.println(opposite);
        } else {
            System.out.println("输入有误");
        }
    }
}

标签:输出,scanner,opposite,StringBuilder,System,num,反向,输入,out
From: https://www.cnblogs.com/faith007/p/17211734.html

相关文章