问题描述:Java 描述,2数相加计算总共进了多少位。
代码如下:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a,b;
while(sc.hasNext()) // 循环读入
{
a = sc.nextInt();
b = sc.nextInt();
int count = 0;
int jw = 0;
while(true)
{
jw = (a % 10 + b % 10 + jw) >= 10 ? 1 : 0; // 取出a,b个位+上进位如果大于十肯定就是进了一位。
count = count + jw; // 进位了count就自增1
a = a / 10; // a,b,同时缩小10倍。
b = b / 10;
if(a == 0 && b == 0) // 如果等于a,b 同时等于0,证明每一都已经参与了判断,就可以退出循环。
{
break;
}
}
System.out.println(count); // 打印最终的结果。
}
}
}
运行结果: