Java 中的整数操作
我在大学里有一项作业要完成,其中一篇文章要求按照以下说明完成。请不要泄露答案,只要引导我走向正确的方向就很棒了。
所有数据均以四位整数形式传输。您的程序应该读取用户输入的四位整数并对其进行加密,如下所示: - 将每个数字替换为该数字加 7 并除以 10 后得到的余数。然后, - 将第一个数字与第三个数字交换,将第二个数字与第四个数字交换。
除了交换指令末尾的数字之外,我可以做任何事情。
非常感谢所有帮助, 谢谢你们!
I have an assignment to be done in college, a piece asks for the instructions below to be done. Please don't give away the answer, simply leading me in the right direction would be awesome.
All the data is transmitted as four-digit integers. You program should read a four-digit integer entered by the user and encrypt it as follows:
- Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then,
- Swap the first digit with the third, and the second with the fourth.
i can do everything but swapping the digits at the end of the instructions.
All help greatly appreciated,
thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先将程序分解为逻辑步骤:
我想您至少可以做到其中的一些,所以请让我们知道您到目前为止所做的事情。
First break down the program into logical steps:
I imagine you can do at least some of that, so let us know what you've done so far.
首先,您需要获取数字中的每个数字并将其存储在单独的变量中(在数组或列表中)。
假设
您的数组如下所示:
然后对于列表中的每个成员,执行以下操作:
列表[i] = (列表[i] + 7) % 10;
然后按照指示交换列表中的元素。编写一个交换函数以便您可以重用它。
然后从数组中构建你的数字。请注意,对数字求和时,数组索引将是 10 的幂。
该解决方案可以轻松扩展到 n 位整数。
First of all, you need to get every digit in the number and store in separate variables (in an array or a list).
Let's say
Your array would look like this:
Then for each member of the list, do this:
list[i] = (list[i] + 7) % 10;
Then swap the elements in the list as directed. Write a swap function so that you can reuse it.
Then construct your number out of the array. Note that array indexes will be the power of 10 when summing up the figures.
This solution will easily scale to n-digit integers.
您要做的第一步是将 4 位整数拆分为各个数字。为此,您需要了解以 10 为基数的数字表示形式。
一旦您了解了如何将数字拆分为各个数字,其余的操作就应该相当容易完成。
The first step you will want to do is to split the 4 digit integer into its individual digits. To do this, you need to understand base 10 representation of numbers.
Once you understand how to split the number into the individual digits, the rest of the operations should be fairly easy to do.