我需要打印完美数字的因素。这是我主要课程的要点:
ArrayList<Integer> perfNums = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter the upperbound: ");
upperbound = in.nextInt();
for (int i = 1; i <= upperbound; i++) {
if (isPerfect(i)) { //boolean to check if number is a perfect number
perfNums.add(i);
}
}
System.out.println("Perfect numbers between 1 and " + upperbound + " are:");
for (int i = 0; i < perfNums.size(); i++) {
System.out.print(perfNums.get(i) + " = ");
printFactor((int)perfNums.get(i));
System.out.println();
}
这是 printFactor 类。
private static void printFactor(int number){
int factor = 1;
while(factor < number){
if (number%factor == 0) System.out.print(factor+ " + ");
//I don't know how to print the + sign otherwise.
factor++;
}
}
这是一个示例输出:
Enter the upperbound: 10000
Perfect numbers between 1 and 10000 are:
6 = 1 + 2 + 3 +
28 = 1 + 2 + 4 + 7 + 14 +
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 +
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064 +
我有它的主要要点,但我一直在努力解决输出问题。由于我的在线提交系统的限制,我的输出需要符合确切的规格。
我的问题是如何打印完整数字的所有因素,但最后删除+号?(例如)6 = 1 + 2 + 3
我不太确定从while循环中打印的许多方法。 for循环会更好地实现我的目标吗?或者是否有其他方法来打印数字因子?
答案 0 :(得分:6)
解决这个问题的最小变化可能是这样的:
private static void printFactor(int number)
System.out.print(1);
int factor = 2;
while (factor<number) {
if (number%factor == 0) System.out.print(" + " + factor);
factor++;
}
}
1始终是一个因素,因此您可以在循环之前打印它,然后将+
添加到每个后续因素中。
答案 1 :(得分:3)
您应该将要打印的输出缓存到StringBuilder
。然后,您可以在打印整个String
之前删除最后一个加号。它也有更好的表现。
private static void printFactor(int number)
{
StringBuilder output = new StringBuilder();
int factor = 1;
while (factor < number)
{
if (number % factor == 0)
output.append(factor + " + ");
factor++;
}
// remove last plus sign
output.deleteCharAt(output.length() - 1);
// print the whole string
System.out.print(output.toString());
}
答案 2 :(得分:2)
由于factor
从值1开始而number % 1 == 0
始终为真,您可以先打印1,然后在factor
中翻转+
和System.out.print
。像这样:
private static void printFactor(int number) {
if(number > 0) {
System.out.print(1);
}
int factor = 2;
while (factor<number) {
if (number % factor == 0) {
System.out.print(" + " + factor);
}
factor++;
}
}
不是最好的解决方案,但它会完成这项工作。
答案 3 :(得分:2)
尝试创建变量String numb
并使用substring
方法,如下所示:
String numb ="";
while(factor<number){
if(number%factor == 0)
numb= numb + factor+ " + ";
factor++;
}
System.out.print(numb.substring(0, numb.trim().length()-1));
答案 4 :(得分:2)
仅仅是为了使用Java 8:)
private static void printFactor(int number){
System.out.println(IntStream.range(1, number)
.filter(p -> number % p == 0)
.mapToObj(i -> String.valueOf(i))
.collect(Collectors.joining(" + ")));
}
答案 5 :(得分:1)
感谢大家的快速回复。你们都是救星,我设法在将来编码时要考虑一些新的东西。
无论如何,在等待回复时,我正在摆弄代码并提出了一个相当不优雅的解决方案,如果有人感兴趣的话。这是主类的变化:
System.out.println("Perfect numbers between 1 and " + upperbound + " are:");
for(int i=0; i<perfNums.size(); i++){
System.out.print(perfNums.get(i) + " = ");
outputString = printFactor2(perfNums.get(i));
if(outStr.endsWith(" + ")) outStr = outStr.substring(0, outStr.length()-3);
//because the submission system would cry foul with even a single extra space
System.out.println(outStr);
}
以下是 printFactor 类的更改:
private static String printFactor2(int number){
String out = "";
int factor = 1;
while(factor<number){
if(number%factor == 0) out += factor + " + ";
factor++;
}
return out;
}
基本上,我所做的是将因子附加到字符串,然后使用substring方法删除尾随+符号。事后来说,我可能应该在printFactor类中调用substring方法。或许像return out.substring(0, out.length()-3);
这样的东西?
尽管如此,谢谢大家!