问题
This is an assignment from a grade 12 computer science class.
The section of the assignment I am having difficulty with reads as follows:
Determine which of the first 20 Fibonacci numbers are prime numbers.
Put a “this is a prime” text notification in the printout from the Basic challenge.
Store the FibPrimes in an array called FibPrimes.
Here is what I have attempted:
Near the bottom, I attempted to make a loop that would print the text notification "This is a prime" if the given FibNum element was equal to a FibPrime element. This did not work. The problem block is identified with a comment. The rest of the program is okay.
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.
}
}
Output with problem block removed:
0
1*
1*
2
3*
5*
8
13*
21*
34
55*
89*
144
233*
377*
610
987*
1597*
2584
4181*
(The stars identify odd numbers - from a different section of the assignment)
Output with problem block remaining:
0
1*
1*
Notice that the rest of the numbers do not print and there is no text notification.
There are likely better ways to solve the problem than I currently have, but I will continue to revise this. Let me know if you need any more information. Thank you.
Thank you @AJNeufeld and @YayPawSi. Using your solutions, I was able to make the program print. Revised Output:
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*
回答1:
This is 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.");
}
}
来源:https://stackoverflow.com/questions/46996036/how-to-print-a-text-notification-beside-prime-fibonacci-numbers