【问题标题】:Print Strings instead of Fibonacci numbers打印字符串而不是斐波那契数
【发布时间】:2016-08-19 16:48:19
【问题描述】:

我有一个关于在 Java 中生成斐波那契数的初学者问题。

在这个java程序中,方法应该打印从BeginIndexLastIndex的斐波那契数列,所以如果数字是5的倍数,它应该打印"Hi"(而不是数字),如果是打印"I am"该数字是 7 的倍数,如果该数字是 35 的倍数,则打印 "Hi I am me"。我不太确定如何执行此操作。

class FibonacciClass {
    public static void main(String args[] ) throws Exception {

        generatefibonacci(10, 20);
        generatefibonacci(0, 1);


    }


      private static void generatefibonacci(int BeginIndex, int LastIndex) {



        }

【问题讨论】:

    标签: java fibonacci


    【解决方案1】:

    另一种可能性:

    private static void generateFibonacci(int beginIndex, int lastIndex) {
    
        int len = lastIndex + 1;
        int[] fib = new int[len];
        fib[0] = 0; 
        fib[1] = 1;
    
        // Building Fibonacci sequence from beginIndex through lastIndex
        for (int i = 2; i < len; ++i)
            fib[i] = fib[i-1] + fib[i-2];
    
        // Printing 
        for (int index = beginIndex; index <= lastIndex; ++index) {
    
            if ((fib[index] % 5 == 0) && (fib[index] % 7 == 0)) {
                System.out.println("Hi I am me");
            }
            else if (fib[index] % 5 == 0) { 
                System.out.println("Hi");
            }
            else if (fib[index] % 7 == 0) {
                System.out.println("I am");
            }
            else {
                System.out.println(fib[index]);
            }
        }
    }
    

    【讨论】:

    • 我认为你应该测试 if (fib[index] % 5 == 0) 而不是 if 索引。您也可以测试第一种情况 if (fib[index] % 35 == 0) 而不是同时测试 7 和 5,但这只是一个小的优化。
    • 是的,我会更正您的帖子。我认为他想知道斐波那契数是否可被 5 或 7 整除,而不是索引是否可被 5 或 7 整除。这是我的假设,否则您的答案看起来不错。
    【解决方案2】:

    您正在寻找的是模运算符,%。它将返回除以操作数的余数。因此,从 if (x % 5 == 0 && x % 7 == 0) 接收一个真值将表示数字 x 是 5 和 7 的倍数。如果这种情况没有通过,您应该使用 else if 语句分别检查 x 是否为 5 的倍数,然后检查 x 是否为 7 的倍数,每个 if 分支调用 System.out.println("x is a multiple of y");

    【讨论】:

      【解决方案3】:

      每次 generatefibonacci 产生结果时,您必须使用模 (%) 检查。

      像这样:

      private static generatefibonnacci(int startIndex, int endIndex){
      int result = -1;
      //do generating stuff
      //and set "result" to generated fibonacci
      //and then
      if(result%5==0){
          System.out.println("Hi");
      } else if(result%7==0){
          System.out.println("Hi I am me!");
      } //and so on
      

      所以这只是一个小例子。 玩得开心

      【讨论】:

        【解决方案4】:
         private static void generatefibonacci(int BeginIndex, int LastIndex) {
             int[] numbers = new int[LastIndex + 2]; //creates an array to put fibonacci numbers in
             numbers[0] = 1; numbers[1] = 1;
             for(int i = 2; i <= LastIndex; i ++){
                 numbers[i] = numbers[i - 1] + numbers[i - 2]; //generates the Fibonacci Sequence
             }
             for(int i = BeginIndex; i <= LastIndex; i ++){
                 if(numbers[i] % 5 == 0 && numbers[i] % 7 == 0){//if the remainder when the numbers/5 equals 0 and the remainder when the numbers/7 equals 0
                     System.out.println("Hello I am me");
                 }
                 else if(numbers[i] % 5 == 0){ //if the remainder when the numbers/5 equals 0
                     System.out.println("I am");
                 }
                 else if(numbers[i] % 7 == 0){ //if the remainder when the numbers/7 equals 0
                     System.out.println("Hi");
                 }
                 else{ //if none of the above three conditions are true
                     System.out.println(numbers[i]);
                 }
             }
        
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-07
          • 1970-01-01
          • 2015-01-04
          • 2012-02-15
          • 2013-04-11
          • 2016-01-21
          相关资源
          最近更新 更多