将数字的所有数字加起来并用Java分别显示数字 [英] Sum all digits of a number and show the digits separately in Java
问题描述
我已经使用了很多这个网站,但是我从未真正写过任何东西.今天,我偶然发现了一个我似乎找不到的解决方案.
问题在于,我有一个 int变量
,其位数未知.要求我对所有这些数字求和,然后打印/显示所有数字并分开显示.
例如,用户输入数字"12345678",最后我需要显示一条消息,说数字1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36".我知道如何将所有这些数字分开并求和,但是我不知道如何保存每个数字然后将其显示出来,因为我不仅不知道正确的数字量,而且我也不知道它们的值要么.
通常这并非难事,但这对我来说是棘手的部分,因为我们还没有学过诸如 arrays
, strings
,索引,等等,我们不允许使用它们.因此,我应该使用最简单的代码来做到这一点.
到目前为止,我有以下内容:
import java.util.Scanner;公共课程编号{公共静态void main(String [] args){整数,结果;扫描仪sc =新的扫描仪(System.in);System.out.println(输入数字:");数字= sc.nextInt();结果= 0;而(数字> 0){结果=结果+(数字%10);数字=数字/10;}System.out.println(这是消息要去的地方.");}}
静态最终Scanner输入= new Scanner(System.in);公共静态void main(String [] args){整数,结果;System.out.println(输入数字:");数字= input.nextInt();结果= 0;System.out.print(数字:");//倒数int reversedNum = 0;while(数字!= 0){reversedNum = reversedNum * 10 +数字%10;数字=数字/10;}//遍历数字并打印出来while(reversedNum> 0){System.out.print((reversedNum%10));结果=结果+(倒数%10);reversedNum = reversedNum/10;如果(reversedNum> 0){System.out.print("+");}}System.out.println("=" +结果);}
这可以按正确的顺序打印出所有数字,而无需使用数组和字符串.
示例:
输入数字:12345数字:1 + 2 + 3 + 4 + 5 = 15成功构建(总时间:5秒)
I've been using this site quite a lot but I've never really wrote anything. Today, I've stumbled upon a problem which solution I can't seem to find.
The problem is that, I have an int variable
with an unknown number of digits. It is asked of me to sum all of those digits and then have it printed/shown_as_a_message with all those digit separated.
For example, the user enters the number "12345678" and in the end I need to have a message saying "The numbers 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36". I know how to separate and sum all of those numbers, but I don't know how to save each number and then have it shown, because not only do I not know the right amount of digits, but I don't know their values either.
Usually that wouldn't be hard to do, but here comes the tricky part for me, since we haven't learned things like arrays
, strings
, indexes, etc., we're not allowed to use them. So, I'm supposed to do it with the simplest type of code possible.
So far, I've got the following:
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
int number, result;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number:");
number = sc.nextInt();
result = 0;
while (number > 0){
result = result + (number % 10);
number = number / 10;
}
System.out.println("This is where the message would go.");
}
}
static final Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int number, result;
System.out.println("Enter number:");
number = input.nextInt();
result = 0;
System.out.print("The numbers: ");
//Reverses the number
int reversedNum = 0;
while (number != 0) {
reversedNum = reversedNum * 10 + number % 10;
number = number / 10;
}
//Iterates over the number and prints it out
while (reversedNum > 0) {
System.out.print((reversedNum % 10));
result = result + (reversedNum % 10);
reversedNum = reversedNum / 10;
if (reversedNum > 0) {
System.out.print(" + ");
}
}
System.out.println(" = " + result);
}
This works to print out all the numbers in the correct order without using arrays and strings.
Example:
Enter number:
12345
The numbers: 1 + 2 + 3 + 4 + 5 = 15
BUILD SUCCESSFUL (total time: 5 seconds)
这篇关于将数字的所有数字加起来并用Java分别显示数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!