如何在主要斐波那契数列旁边打印文本通知?
How to print a text notification beside prime fibonacci numbers?
这是 12 年级计算机科学的作业 class。
作业中我遇到困难的部分如下:
判断前 20 个斐波那契数中哪些是素数。
在 Basic 挑战的打印输出中添加“这是素数”文本通知。
将 FibPrime 存储在名为 FibPrime 的数组中。
这是我尝试过的:
在底部附近,我尝试创建一个循环,如果给定的 FibNum 元素等于 FibPrime 元素,该循环将打印文本通知 "This is a prime"。这没有用。 问题块用注释标识。程序的其余部分没问题。
package fibonaccinumbers;
public class FibonacciNumbers {
public static void main(String[] args) {
// Creation of Fibonacci Numbers Array.
int [] FibNums = new int[20];
FibNums[0] = 0;
FibNums[1] = 1;
// Creation if Fibonacci Primes Array.
int [] FibPrimes = new int[7];
FibPrimes[0] = 2;
FibPrimes[1] = 3;
FibPrimes[2] = 5;
FibPrimes[3] = 13;
FibPrimes[4] = 89;
FibPrimes[5] = 233;
FibPrimes[6] = 1597;
// Printing first two fibonacci numbers.
System.out.println(0);
System.out.println(1 + "*");
// Printing remaining fibonacci numbers up to 20th term.
for (int i=2; i<FibNums.length;i++){ // Begin number generation loop.
FibNums[i] = FibNums[i-1] + FibNums[i-2];
// Checks if the fibonacci number is odd.
// A number is not odd if two divides into it evenly.
boolean oddcheck = true;
if (FibNums[i]%2==0){
oddcheck = false;
}
// Prints odd fibonacci numbers with a star beside it.
// Prints even fibonacci numbers with no star beside it.
if (oddcheck == true){
System.out.println(FibNums[i] + "*");
} else {
System.out.println(FibNums[i]);
}
// PROBLEM BLOCK HERE. ************************
// If any element in the FibPrimes array is equal to the FibNums
// array, then the number is a prime.
for (int n=0; n<=FibPrimes.length; n++){
if (FibNums[i] == FibPrimes[n]){
System.out.print(" " + "This is a prime.");
}
}
} // End number generation loop.
}
}
删除了问题块的输出:
0
1*
1*
2
3*
5*
8
13*
21*
34
55*
89*
144
233*
377*
610
987*
1597*
2584
4181*
(星星标识奇数 - 来自作业的不同部分)
剩余问题块的输出:
0
1*
1*
请注意,其余数字不会打印,也没有文本通知。
可能有比我现在更好的方法来解决这个问题,但我会继续修改它。如果您需要更多信息,请告诉我。谢谢。
谢谢@AJNeufeld 和@YayPawSi。使用您的解决方案,我能够打印程序。
修改后的输出:
0
1*
1*
This is a prime. 2
This is a prime. 3*
This is a prime. 5*
8
This is a prime. 13*
21*
34
55*
This is a prime. 89*
144
This is a prime. 233*
377*
610
987*
This is a prime. 1597*
2584
4181*
这是 ArrayIndexOutOfBoundException,
//Remove = sign for n < FibPrimes.length
for (int n = 0; n < FibPrimes.length; n++){
if (FibNums[i] == FibPrimes[n]){
System.out.print(" " + "This is a prime.");
}
}
这是 12 年级计算机科学的作业 class。
作业中我遇到困难的部分如下:
判断前 20 个斐波那契数中哪些是素数。
在 Basic 挑战的打印输出中添加“这是素数”文本通知。
将 FibPrime 存储在名为 FibPrime 的数组中。
这是我尝试过的:
在底部附近,我尝试创建一个循环,如果给定的 FibNum 元素等于 FibPrime 元素,该循环将打印文本通知 "This is a prime"。这没有用。 问题块用注释标识。程序的其余部分没问题。
package fibonaccinumbers;
public class FibonacciNumbers {
public static void main(String[] args) {
// Creation of Fibonacci Numbers Array.
int [] FibNums = new int[20];
FibNums[0] = 0;
FibNums[1] = 1;
// Creation if Fibonacci Primes Array.
int [] FibPrimes = new int[7];
FibPrimes[0] = 2;
FibPrimes[1] = 3;
FibPrimes[2] = 5;
FibPrimes[3] = 13;
FibPrimes[4] = 89;
FibPrimes[5] = 233;
FibPrimes[6] = 1597;
// Printing first two fibonacci numbers.
System.out.println(0);
System.out.println(1 + "*");
// Printing remaining fibonacci numbers up to 20th term.
for (int i=2; i<FibNums.length;i++){ // Begin number generation loop.
FibNums[i] = FibNums[i-1] + FibNums[i-2];
// Checks if the fibonacci number is odd.
// A number is not odd if two divides into it evenly.
boolean oddcheck = true;
if (FibNums[i]%2==0){
oddcheck = false;
}
// Prints odd fibonacci numbers with a star beside it.
// Prints even fibonacci numbers with no star beside it.
if (oddcheck == true){
System.out.println(FibNums[i] + "*");
} else {
System.out.println(FibNums[i]);
}
// PROBLEM BLOCK HERE. ************************
// If any element in the FibPrimes array is equal to the FibNums
// array, then the number is a prime.
for (int n=0; n<=FibPrimes.length; n++){
if (FibNums[i] == FibPrimes[n]){
System.out.print(" " + "This is a prime.");
}
}
} // End number generation loop.
}
}
删除了问题块的输出:
0
1*
1*
2
3*
5*
8
13*
21*
34
55*
89*
144
233*
377*
610
987*
1597*
2584
4181*
(星星标识奇数 - 来自作业的不同部分)
剩余问题块的输出:
0
1*
1*
请注意,其余数字不会打印,也没有文本通知。
可能有比我现在更好的方法来解决这个问题,但我会继续修改它。如果您需要更多信息,请告诉我。谢谢。
谢谢@AJNeufeld 和@YayPawSi。使用您的解决方案,我能够打印程序。 修改后的输出:
0
1*
1*
This is a prime. 2
This is a prime. 3*
This is a prime. 5*
8
This is a prime. 13*
21*
34
55*
This is a prime. 89*
144
This is a prime. 233*
377*
610
987*
This is a prime. 1597*
2584
4181*
这是 ArrayIndexOutOfBoundException,
//Remove = sign for n < FibPrimes.length
for (int n = 0; n < FibPrimes.length; n++){
if (FibNums[i] == FibPrimes[n]){
System.out.print(" " + "This is a prime.");
}
}