2017-07-10 4 views
1

J'essaye d'imprimer une quantité finie d'une séquence de tribonacci (où la valeur de n'importe quel terme est égale à la somme des trois termes précédents ie [2,3,5,10,18,33 ...]) en utilisant les valeurs de départ obtenues à partir de l'entrée de l'utilisateur. L'utilisation d'autres valeurs positives en plus 0 et 1, le code fonctionne toujours, comme indiqué ci-dessous:Pourquoi cet algorithme imprime-t-il une séquence fibonacci/tribonacci pour toutes les graines sauf 0 et 1?

2 
3 
5 
Printing... 
[2, 3, 5, 10, 18, 33, 61, 112, 206, 379, 697, 1282, 2358, 4337, 7977, 14672, 26986, 49635, 91293, 167914] 
Process finished with exit code 0 

Mais avec 0, 1 et 1 les graines, je reçois:

0 
1 
1 
Printing... 
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
Process finished with exit code 0 

Voici ce le code (je suis au courant des commentaires trop explicites, il est pour une raison):

import java.util.Arrays; 
import java.util.Scanner; 

public class Tribonacci { 

public static void main(String[] args) throws InterruptedException { 
    System.out.printf("Hello!\n"); 
    Thread.sleep(2000); 
    System.out.printf("The purpose of this program is to display the first twenty terms of either a Fibonacci sequence or a Tribonacci sequence\n"); 
    System.out.printf("To start, type \"F\" for Fibonacci or \"T\" for Tribonacci:\n");//Give prompt options 
    Scanner seqeuence = new Scanner(System.in); 
    String typeseq = seqeuence.next();//get user input, based on^prompt^
    printSequence(typeseq); 
} 

private static void printSequence(String typeOfSequence) throws InterruptedException { 


    //checks for which kind of sequence they want to print based on above prompt 
    switch (typeOfSequence) { 
     case "T"://if user types "T" 

      System.out.printf("You have chosen to print the first twenty terms of the Tribonacci sequence\n"); 
      Thread.sleep(2000); 
      System.out.printf("We will need the seed terms for this to work, so input THREE non-negative integers, in numerical order:\n"); 
      Scanner getTrib = new Scanner(System.in); 
      int[] tSeeds = new int[3];//scans for three seeds from the user, stores in an array of size 3 

      //put each seed term into an array, in order to perform arithmetic on the elements later 
      for (int i = 0; i < tSeeds.length; i++) { 
       tSeeds[i] = getTrib.nextInt(); 
      } 

      int[] newTSeq = new int[20];//final array to be printed 
      newTSeq[0] = tSeeds[0]; 
      newTSeq[1] = tSeeds[1]; 
      newTSeq[2] = tSeeds[2]; 
      int incrementT = 0;//used to traverse the array and move which seeds should be summed 
      for (int itemT : newTSeq) { 

       if (itemT == tSeeds[0] || itemT == tSeeds[1] || itemT == tSeeds[2]) { 
        continue; 

       } else { 
        newTSeq[3 + incrementT] = newTSeq[incrementT] + newTSeq[incrementT + 1] + newTSeq[incrementT + 2]; 
        ++ incrementT; 
       } 
      } 

      System.out.printf("Printing...\n"); 
      Thread.sleep(2000); 
      System.out.print(Arrays.toString(newTSeq));//terms are converted into strings to be printed 

      break; 

     case "F"://if user types "F" 

      System.out.printf("You have chosen to print the first twenty terms of the Fibonacci sequence\n"); 
      Thread.sleep(2000); 
      System.out.printf("We will need the seed terms for this to work, so input TWO non-negative integers, in numerical order:\n"); 
      Scanner getFib = new Scanner(System.in); 
      int[] fSeeds = new int[2];//scan user input of TWO integers into an array that holds TWO integers 

      //put each seed term into array for later computation, like above 
      for (int i = 0; i < fSeeds.length; i++) { 
       fSeeds[i] = getFib.nextInt(); 
      } 

      int[] newFSeq = new int[20];//final array to be printed 
      newFSeq[0] = fSeeds[0]; 
      newFSeq[1] = fSeeds[1]; 
      int incrementF = 0; 
      for (int itemF : newFSeq) { 
       if (itemF == newFSeq[0] || itemF == newFSeq[1]) { //if the iterator is on one of the seeds, don't sum 
        continue; 
       } else { 
        newFSeq[2 + incrementF] = newFSeq[incrementF] + newFSeq[incrementF + 1];//sums the two preceding terms 
        incrementF++; 
       } 
      } 

      System.out.printf("Printing...\n"); 
      Thread.sleep(2000); 
      System.out.print(Arrays.toString(newFSeq));//convert all array values to strings, to be printed 

      break; 

     default://if neither case is satisfied, print this message 
      System.out.printf("Restart the program. Next time, input the SUGGESTED prompt items \"F\" for Fibonacci or \"T\" for Tribonacci"); 
    } 
} 
} 
+2

Parce que 'si (itemT == tSeeds [0] || itemT == tSeeds [1] || itemT == tSeeds [2]) {continuer; '. Maintenant, la question est: pourquoi avez-vous mis cette ligne là du tout? –

+1

Vous avez trop compliqué votre boucle 'for'. Utilisez juste un 'for' normal avec le compteur commençant à 3 et allant à la fin. – v010dya

+0

@ v010dya Merci beaucoup. Je ne sais pas comment j'ai décidé d'une solution aussi compliquée. Fonctionne très bien. –

Répondre

1

Eh bien, vous avez cette condition étrange

if (itemT == tSeeds[0] || itemT == tSeeds[1] || itemT == tSeeds[2]) { 
    continue; 
} 

qui signifie que vous n'attribuez rien au tableau newTSeq si l'un des tSeeds[0], tSeeds[1] ou tSeeds[2] est 0 (puisque itemT est 0 par défaut pour tous les éléments de newTSeq, et dans le cas de 0,1,1 entrée vous changez seulement newTSeq[1] et newTSeq[2] à 1 avant votre boucle).

Je ne suis pas sûr de ce que cette condition est censée faire. Vous pouvez probablement l'enlever.

-1

Il suffit de remplacer le code suivant: -

int[] newTSeq = new int[20];//final array to be printed 
Arrays.fill(newTSeq, -1); 
newTSeq[0] = tSeeds[0]; 
newTSeq[1] = tSeeds[1]; 
newTSeq[2] = tSeeds[2];