从循环中打印
Printing from a loop
我需要打印一个完美数的因数。这是我的主要内容 class:
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 class.
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 循环是否更适合我的目标?或者是否有其他方法来打印数字的因数?
您应该将要打印的输出缓存到 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());
}
解决这个问题的最少更改可能是这样的:
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 始终是一个因素,因此您可以在循环之前打印它,然后将 +
添加到每个后续因素。
由于 factor
从值 1 开始并且 number % 1 == 0
将始终为真,您可以先打印 1 然后翻转 factor
和 +
in 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++;
}
}
不是最好的解决方案,但它可以完成工作。
尝试创建一个变量字符串 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));
感谢大家的快速回复。你们都是救命恩人,我在以后编写代码时设法挑选了一些新的东西来考虑。
无论如何,在等待回复的过程中,我一直在摆弄代码,并想出了一个相当不优雅的解决方案,如果有人感兴趣的话。这是对主要 class:
的更改
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 class 的更改:
private static String printFactor2(int number){
String out = "";
int factor = 1;
while(factor<number){
if(number%factor == 0) out += factor + " + ";
factor++;
}
return out;
}
基本上,我所做的是将因子附加到字符串,然后使用子字符串方法删除尾随的 + 号。事后看来,我可能应该改为在 printFactor class 中调用 substring 方法。也许像 return out.substring(0, out.length()-3);
?
不过还是谢谢大家!
只是为了使用 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(" + ")));
}
我需要打印一个完美数的因数。这是我的主要内容 class:
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 class.
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 循环是否更适合我的目标?或者是否有其他方法来打印数字的因数?
您应该将要打印的输出缓存到 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());
}
解决这个问题的最少更改可能是这样的:
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 始终是一个因素,因此您可以在循环之前打印它,然后将 +
添加到每个后续因素。
由于 factor
从值 1 开始并且 number % 1 == 0
将始终为真,您可以先打印 1 然后翻转 factor
和 +
in 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++;
}
}
不是最好的解决方案,但它可以完成工作。
尝试创建一个变量字符串 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));
感谢大家的快速回复。你们都是救命恩人,我在以后编写代码时设法挑选了一些新的东西来考虑。
无论如何,在等待回复的过程中,我一直在摆弄代码,并想出了一个相当不优雅的解决方案,如果有人感兴趣的话。这是对主要 class:
的更改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 class 的更改:
private static String printFactor2(int number){
String out = "";
int factor = 1;
while(factor<number){
if(number%factor == 0) out += factor + " + ";
factor++;
}
return out;
}
基本上,我所做的是将因子附加到字符串,然后使用子字符串方法删除尾随的 + 号。事后看来,我可能应该改为在 printFactor class 中调用 substring 方法。也许像 return out.substring(0, out.length()-3);
?
不过还是谢谢大家!
只是为了使用 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(" + ")));
}