dimanche 26 juin 2016

FizzBuzz Using Arrays (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.

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;

  //2b. Initialize two String arrays called 'fizz' and 'buzz.'
  public static String[] fizz;
  public static String[] buzz;
  public static String[] fizzbuzz;

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

  public static int[] list() {

    anArray = new int[10];
    //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 n: anArray) {
      System.out.println(n + " ");
    }
    for (String f : fizzbuzz) { 
      System.out.println(f + " ");
    }
  }

  public static int randomFill() {

    Random rand = new Random();
    randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
  }

  public static String[] GetFizzOrBuzz() {

    fizzbuzz = new String[10];

    int x = 0;
    int counter;

    for(counter = 0; counter < fizzbuzz.length; counter++) {
      if(randomNum % 3 == 0) {
      x = 1;
    }
      if(randomNum % 5 == 0) {
      x = x + 2;
    }
    switch(x) {
      case 1:
        fizzbuzz[counter] = "fizz";
         break;
      case 2:
        fizzbuzz[counter] = "buzz";
      case 3:
        fizzbuzz[counter] = "fizzbuzz";
    }
  }
    return fizzbuzz; 
  }

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

Issue: I'm receiving a null value for all of my String array, fizzbuzz. Why isn't my program returning the design result, or rather, what am I doing wrong?

EDIT I currently had my min/max values set to 3 for purposes of testing my returns. Ideally, I'd like it to function with min/max 0-100. Also, the program works with 3 and 15 currently, but not many other numbers, haha.




Aucun commentaire:

Enregistrer un commentaire