如何在斐波那契素数旁边打印文本通知? [英] How to print a text notification beside prime fibonacci numbers?

查看:73
本文介绍了如何在斐波那契素数旁边打印文本通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是12年级计算机科学课的作业.

This is an assignment from a grade 12 computer science class.

我遇到困难的作业部分内容如下:

The section of the assignment I am having difficulty with reads as follows:

  • 确定前20个斐波纳契数中的哪一个是质数.

  • Determine which of the first 20 Fibonacci numbers are prime numbers.

在基本"挑战的打印输出中添加这是主要的"文本通知.

将FibPrimes存储在名为FibPrimes的数组中.

Store the FibPrimes in an array called FibPrimes.

这是我尝试过的:

在底部附近,我尝试创建一个循环,如果给定的FibNum元素等于FibPrime元素,则将打印文本通知"This is a prime".这没有用. 该问题块带有注释.该程序的其余部分都可以.

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.

    }

}

已删除问题块的输出:

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)

剩余问题块的输出:

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.

谢谢@AJNeufeld和@YayPawSi.使用您的解决方案,我能够打印程序. 修改后的输出:

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*

推荐答案

这是ArrayIndexOutOfBoundException,

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.");
            }
     }

这篇关于如何在斐波那契素数旁边打印文本通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆