gpt4 book ai didi

java - 反数是怎么得来的?

转载 作者:行者123 更新时间:2023-12-01 21:58:27 25 4
gpt4 key购买 nike

我不明白使用这行代码的含义

这是在 while block 上。

import java.util.Scanner;

public class ReverseNumberWhile {
public static void main(String[] args){

int num = 0;
int reversenum = 0;

System.out.println("Enter your number and press enter: ");

Scanner input = new Scanner(System.in);
num = input.nextInt();

while(num != 0) {
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}

System.out.println("Reverse of input number is: " + reversenum);
}
}

最佳答案

就这样,评论:

 while(num != 0)
{
reversenum = reversenum * 10; //This moves the reversenum attained in previous iteration to one higher place value
reversenum = reversenum + num%10; // num%10 gives you the last digit which is added to the existing reversenum
num = num/10; //This leaves the remaining digits removing rightmost
}

关于java - 反数是怎么得来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com