在单个值中交换特定的两个值 [英] swapping the particular two values in the single value

查看:66
本文介绍了在单个值中交换特定的两个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

O / p

输入值456239875

输入交换值4 9

交换后

956234875

O/p
Enter the value 456239875
Enter the swapping value 4 9
After swapping
956234875

推荐答案

将每个数字记录到一个数组中,获取要从数组交换的数字的索引,并使用临时变量交换它们。
log each digit into an array, get the indices of the digits to be swapped from the array, and swap them using a temporary variable.


像建议的那样使用数字的字符串表示,例如

Like suggested use the string representation of the number, e.g.
String s = String.valueOf(456239875);
s=s.replace('4','x');
s=s.replace('9','4');
s=s.replace('x','9');
int result = Integer.parseInt(s);
System.out.println(result);


这篇关于在单个值中交换特定的两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆