mardi 28 juin 2016

Why is my case switch statement filling my arrays with only null, fizz, buzz, or fizzbuzz? (JAVA)

Preface: This is a homework assignment; I'm not looking for you to do it for me. However, any suggestions or related examples would be greatly appreciated.

Issue: I'm having difficulty using the RANDOM utility in JAVA. My output in my program isn't currently following the guidelines of my homework, but I'm uncertain on how to get the desired result accurately. Currently, the program tends to inaccurately put fizz, buzz, or fizzbuzz beside most fields, and I'm unsure what I'm doing wrong.

Assignment:

You can do this all in the main method. This is using a game called Fizz-Buzz, an ancient programmer’s game.

  1. Start by initializing some variables to set the maximum and minimum value of a random number and for the capacity of an array. (20/100)

  2. Initialize three new arrays, one for a list of random numbers (as integers) and two for String arrays called ‘fizz’ and ‘buzz’.(20/100)

  3. You’ll also need an integer for counting.

  4. Write a for loop that generates a random number for each position in the array. Remember that the range for this will be set by the two variables initialized at the beginning of the file. There are multiple ways to create a random number, just find one that works for you. (20/100)

  5. Using the count of the arrays, create another array that will store all of the fizzes and buzzes without any extra space leftover in the array. (20/100)

  6. Use a for each loop to iterate the array and print all of the fizzes and buzzes, with no other output. (20/100)

What I've accomplished thus far:

        import java.util.Random;

/**
 *
 * @author
 */
public class FizzBuzz {

  //2a. Initialize one int array for a list of random numbers.
  private static int[] anArray;
  private static final int size = 10;

  //2b. Initialize two String arrays called 'fizz' and 'buzz.'
  public static String[] fizz;
  public static String[] buzz;
  public static String[] fizzbuzz;
  public static Random rand = new Random();

  //1. Set the maximum and minimum value of a random number.
  private static final int min = 0;
  private static final int max = 5;
  private static int count = 0;

  public static int[] list() {

    anArray = new int[size];
    //3. Make an integer for counting("counter" in the for loop)
    //4. Write a for loop that generates a random number for
    //   each position in the array.
    for(count = 0; count < anArray.length; count++) {
      anArray[count] = randomFill();
    }
    return anArray;
  }

  public static void print() {

    for (int i = 0; i < anArray.length; i++) {
      System.out.println(anArray[i] + ": " + fizz[i] + buzz[i]);
    }
  }

  public static int randomFill() {  
    return rand.nextInt((max - min) + 1) + min;
  }

  public static String[] getF() {

    fizz = new String[size];

    int x = 0;
    int counter;

    for(counter = 0; counter < fizz.length; counter++) {
      if(anArray[counter] % 3 == 0) {
      x = 1;
    } else {
        fizz[counter] = "";
      }

    switch(x) {
      case 1:
        fizz[counter] = "fizz";
         break;
    }
  }
    return fizz; 
  }
  public static String[] getB() {
    buzz = new String[size];

    int x = 0;
    int counter;

    for(counter = 0; counter < buzz.length; counter++) {

      if(anArray[counter] % 5 == 0) {
      x = 2;
    } else {
        buzz[counter] = "";
      }
    switch(x) {
      case 2:
        buzz[counter] = "buzz";
    }
  } 
    return buzz;
  }

  public static String[] getFB() {
    fizzbuzz = new String[size];



    return fizzbuzz;
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    list();
    getF();
    getB();
    print();
  } 
}

Aucun commentaire:

Enregistrer un commentaire