【学习日志】-题:MT1027~1028【有关逆序输出问题】

文章介绍了如何在Java中实现数字和字符串的逆序输出,包括取模输出、遍历输出、使用toCharArray方法和reverse方法。特别指出,代数相加法在处理整十数时会有问题,而其他方法能有效避免此问题。

前言(问题引入)

要求:编写一个程序,要求输入一个两位数的数字,然后逆序输出数字。
不考虑不合理的输入或是溢出等特殊情况。
样例:
输入:28
输出:82
public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        //用代数方法:1.得到个位:28/10 = 2 2.得到十位:28%10*10 = 80 3.相加
        System.out.println(a / 10 + a % 10 * 10);

        input.close();
    }

运行之后,发现用代数方法存在一些问题:遇到整十数,会把取模后的零与个位数加上,得到一个个位数。 e.g.:输入: 20 ; 输出: 2


逆序输出的方法讨论归纳(解决办法)

方法一:遍历输出(相关:String转为char)每一位逆序输出

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
            String num = input.nextLine();
            for (int i = num.length() - 1; i >= 0; i--) {
                char number = num.charAt(i);
               System.out.print(number);
            } 
}

方法二:取模输出(每一位

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        while (num1 != 0) {
            int a = num1 % 10;
            System.out.print(a);
            num1 = num1 / 10;
        }
            }

方法三:toCharArray()方法(得到每一位

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String num2 = input.nextLine();
        String number = "";
        char[] c = num.toCharArray();
           for (int i = c.length - 1; i >= 0; i--){
               number += c[i];
     }
           System.out.print(number);
            }

方法四:reverse方法

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String number = input.nextLine();
        System.out.print(reverse(number));
    }
    private static String reverse(String number){
            return new StringBuffer(number).reverse().toString();
    }

应用

类型相似

数字逆序输出

要求:将一个四位数正整数(如1278)逆序(如8721)输出。
不考虑负数等特殊情况。
样例:
输入:1234
输出:4321
public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
      String num = input.nextLine();
        for (int i = num.length() - 1; i >= 0; i--) {
            char number = num.charAt(i);
            System.out.print(number);
        }
      input.close();
   }

类型扩大

字符串类型,如中文、英文…逆序输出

要求:
输入: I am a student
输出: tneduts a ma I
public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        for (int i = str.length()-1;i >= 0; i--) {
            System.out.print(str.charAt(i));
        }
    }

总结

总结:
代数相加后再输出在遇到整十数逆序输出,就会变成个位数输出,问题关注每一位逆序输出,
方法二(取模输出)不进行代数相加,每一次得到的一位数直接进行输出,再回去判断,可以解决问题。
还可以用遍历输出、toCharArray()方法和reverse方法进行逆序输出。
数字、中文、英文逆序输出都可以用类似办法。
[2025-08-22 15:30:00] [ 724.292851] CPU: 0 PID: 18692 Comm: rmmod Tainted: P 5.4.281 #0 [2025-08-22 15:30:00] [ 724.300229] Hardware name: MediaTek MT7987A SPIM-NAND RFB (DT) [2025-08-22 15:30:00] [ 724.306048] pstate: 80000005 (Nzcv daif -PAN -UAO) [2025-08-22 15:30:00] [ 724.310831] pc : hooks_validate+0x34/0x70 [2025-08-22 15:30:00] [ 724.314828] lr : __nf_hook_entries_try_shrink+0xe4/0x130 [2025-08-22 15:30:00] [ 724.320125] sp : ffffffc011923d00 [2025-08-22 15:30:00] [ 724.323427] x29: ffffffc011923d00 x28: ffffff801e250ac0 [2025-08-22 15:30:00] [ 724.328724] x27: 0000000000000000 x26: 0000000000000000 [2025-08-22 15:30:00] [ 724.334022] x25: 0000000056000000 x24: ffffffc010af1940 [2025-08-22 15:30:00] [ 724.339320] x23: ffffff8012518c00 x22: ffffffc010af2290 [2025-08-22 15:30:00] [ 724.344617] x21: ffffffc010870738 x20: ffffff8014ec7600 [2025-08-22 15:30:00] [ 724.349916] x19: ffffff8014ec7658 x18: 0000000000000000 [2025-08-22 15:30:00] [ 724.355213] x17: 0000000000000000 x16: 0000000000000000 [2025-08-22 15:30:00] [ 724.360511] x15: 0000000000000000 x14: 0000000000000000 [2025-08-22 15:30:00] [ 724.365809] x13: 0000000000000000 x12: 0000000000000000 [2025-08-22 15:30:00] [ 724.371107] x11: 0000000000000000 x10: 0000000000000000 [2025-08-22 15:30:00] [ 724.376405] x9 : 0000000000000000 x8 : ffffff8012518c80 [2025-08-22 15:30:00] [ 724.381703] x7 : 0000000000000048 x6 : 0000000000000060 [2025-08-22 15:30:00] [ 724.387001] x5 : ffffffc010870738 x4 : ffffff8012518c48 [2025-08-22 15:30:00] [ 724.392299] x3 : ffffffc0088de010 x2 : 000000007fffffff [2025-08-22 15:30:00] [ 724.397597] x1 : 0000000000000003 x0 : ffffff8012518c00 [2025-08-22 15:30:00] [ 724.402895] Call trace: [2025-08-22 15:30:00] [ 724.405331] hooks_validate+0x34/0x70 [2025-08-22 15:30:00] [ 724.408981] __nf_unregister_net_hook+0x128/0x198 [2025-08-22 15:30:00] [ 724.413672] nf_unregister_net_hook+0x24/0x60 [2025-08-22 15:30:00] [ 724.418018] _6+0x3c/0x188 [app_dpi] [2025-08-22 15:30:00] [ 724.421585] __arm64_sys_delete_module+0x1b0/0x258 [2025-08-22 15:30:00] [ 724.426365] el0_svc_common.constprop.2+0x7c/0x110 [2025-08-22 15:30:00] [ 724.431142] el0_svc_handler+0x20/0x80 [2025-08-22 15:30:00] [ 724.434879] el0_svc+0x8/0x6c0 [2025-08-22 15:30:00] [ 724.437924] Code: d503201f f8617883 eb05007f 540000c0 (b9402063) [2025-08-22 15:30:00] [ 724.444003] ---[ end trace fb8c5245bc54a355 ]--- [2025-08-22 15:30:00] [ 724.452475] Kernel panic - not syncing: Fatal exception [2025-08-22 15:30:00] [ 724.457690] SMP: stopping secondary CPUs [2025-08-22 15:30:00] [ 724.461602] Kernel Offset: disabled [2025-08-22 15:30:00] [ 724.465078] CPU features: 0x00002,2000200c [2025-08-22 15:30:00] [ 724.469161] Memory Limit: none [2025-08-22 15:30:00] [ 724.476138] Starting crashdump kernel... [2025-08-22 15:30:00] [ 724.480059] Starting Memory dump SMCC [2025-08-22 15:30:00] [ 724.483716] Memory dump SMCC failed [2025-08-22 15:30:00] [ 724.487191] Rebooting in 3 seconds.. [2025-08-22 15:30:00] [ 728.490756] SMP: failed to stop secondary CPUs 0-3 [2025-08-22 15:30:04] [2025-08-22 15:30:04]
08-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值