gpt4 book ai didi

java - 尝试使用 int Java 反转 123 _> 321

转载 作者:行者123 更新时间:2023-12-01 17:52:48 25 4
gpt4 key购买 nike

我的任务是创建一个方法,该方法将从用户处获取 3 位数字输入并反转其顺序,然后以新顺序将其返回,而不将任何内容转换为字符串,它全部保留为 int。我无法弄清楚如何在不将值相加的情况下返回新数字(第一第二第三);

public class Lab01
{

public int sumTheDigits(int num)
{
int one;
int two;
int three;

one = num % 10;
two = (num/10) % 10;
three = num / 100;

return one + two + three;
}

public int reverseTheOrder(int num)
{
int first;
int second;
int third;

third = num / 100;
second = (num/10) % 10;
first = num % 10;


return ?;
}

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

Lab01 lab = new Lab01();
System.out.println("Enter a three digit number: ");
int theNum = input.nextInt();
int theSum = lab.sumTheDigits(theNum);
int theReverse = lab.reverseTheOrder(theSum);

System.out.println("The sum of the digits of " + theNum + " is " + theSum);
System.,out.println(theNum + " reversed is " + theReverse);



}

}


最佳答案

您可以尝试以下任意数字长度

public static void main(String args[]) {
int num = 0;
int reversenum = 0;
System.out.println("Enter number: ");
Scanner in = new Scanner(System.in);
num = in.nextInt();
while (num != 0) {
reversenum = reversenum * 10;
reversenum = reversenum + num % 10;
num = num / 10;
}

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

关于java - 尝试使用 int Java 反转 123 _> 321,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60768025/

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