【发布时间】:2014-05-09 11:07:55
【问题描述】:
我是一个苦苦挣扎的新手,试图完成一个程序分配,并且我的输出重复了一个整数的数字,应该在一行上,但间隔(正确的例子:1234 的数字是:1 2 3 4)。这是错误输出的示例,下面是我的程序。任何指导将不胜感激。
输入一个整数:1234
1234的位数是:1
1234的位数是:2
1234的位数是:3
1234的位数是:4
数字之和 = 10
我的程序:
import java.util.*;
public class IntegertoIndividualtoSum
{
public static void main(String[] args)
{
long integer, digit, sum = 0;
char digitAt;
String Stringnum;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter an integer: ");
integer = keyboard.nextLong();
Stringnum = String.valueOf(Math.abs(integer));
for (int count = 0; count < Stringnum.length(); count++)
{
digitAt = Stringnum.charAt(count);
digit = Character.getNumericValue(digitAt);
sum += digit;
System.out.println("The digits of " + integer + " are: " + digit);
}
System.out.println("The sum of the digits = " + sum);
}
}
【问题讨论】:
-
should be together on one line, but spaced是什么阻止你这样做? -
@SotiriosDelimanolis 我整天盯着屏幕想弄明白。
-
不要只是盯着它,要分析它。如何打印空格?
println是做什么的?查看javadoc。 Java 有很好的文档记录。 -
@SotiriosDelimanolis 嘿,感谢您的帮助,这是漫长的一天......
-
@SotiriosDelimanolis 必须同意 SolomonSky 的观点......你的态度是多余的。无论我们认为问题多么简单,都没有必要贬低他人。
标签: java string loops integer converter