3-4-4 整数分解-逆序输出

352 % 10-->2             (n % 10 --> 个位)

352 /10 -->35

35%10 -->5

35 / 10 -->3

3 % 10 -->3

3 / 10 --> 0(终止符)


逆序输出做法:

1.

import java.util.Scanner;

public class Hello {    
    
    public static void main(String[] args) {                  
        Scanner in = new Scanner(System.in);
    	int number ; 
    	number = in.nextInt();
    	
    	do{
    		int digit = number % 10;
    		<span style="color:#ff0000;">System.out.print(digit);</span>
    		number = number / 10;
    		
    	} while (number > 0);
    	<span style="color:#ff0000;">System.out.println();</span>
    	
    	
    }  
}    
红字为新


缺点:输入700,输出007



2.

public class Hello {    
    
    public static void main(String[] args) {                  
        Scanner in = new Scanner(System.in);
    	int number ; 
    	number = in.nextInt();
    	<span style="color:#3366ff;">int result = 0;</span>
    	
    	do{
    		int digit = number % 10;
    		<span style="color:#3366ff;">result = result * 10+ digit;
</span>    		<span style="color:#ff0000;">System.out.print(digit);</span>
    		number = number / 10;
    		
    	} while (number > 0);
    	System.out.println();
    	<span style="color:#ff0000;">System.out.println(result);</span>
    	
    }  
}    

输入700,输出007,7



假如程序一开始用while

while (number > 0)
    	{
    		int digit = number % 10;
    		result = result * 10+ digit;
    		System.out.print(digit);
    		number = number / 10;
    		
    	} 
    	System.out.println();
    	System.out.println(result);


输入700无问题,但是输入0出现问题,下星期再介绍





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值