如何将数组值存储到新数组(JAVA)

时间:2016-06-27 01:42:02

标签: java arrays random

问题:我已完成此作业的第1-4步。但是,我目前仍然坚持这项任务的第5步和第6步,所以我对如何将我的fizz和buzz String数组合成一个单独的fizzbuzz String数组感到茫然。

TL; DR我不知道如何做第五步和第六步。

分配:

  

您可以在main方法中执行此操作。这是使用一个叫做的游戏   Fizz-Buzz,一个古老的程序员的游戏。

     
      
  1. 首先初始化一些变量,以设置随机数的最大值和最小值以及数组的容量。   (20/100)

  2.   
  3. 初始化三个新数组,一个用于随机数列表(作为整数),两个用于名为'fizz'的字符串数组   “嗡嗡”。(20/100)

  4.   
  5. 您还需要一个整数来计算。

  6.   
  7. 编写一个for循环,为数组中的每个位置生成一个随机数。请记住,此范围将设置为   在文件开头初始化的两个变量。有   创建随机数的多种方法,只需找到适用的随机数   您。 (20/100)

  8.   
  9. 使用数组的计数,创建另一个数组,该数组将存储所有的fizzes和buzzes,而不会留下任何额外的空间   数组。 (20/100)

  10.   
  11. 使用a for each循环迭代数组并打印所有的fizzes和buzzes,没有其他输出。 (20/100)

  12.   

到目前为止我所取得的成就:

            /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
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) {
      fizz[counter] = "fizz";
    } else {
        fizz[counter] = "";
      }
  }
    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) {
      buzz[counter] = "buzz";
    } else {
        buzz[counter] = "";
      }
  } 
    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();
  } 
}

1 个答案:

答案 0 :(得分:0)

首先:

Random如何运作

Random的实施工作原因很简单:
如果调用default-constructor,PRNG的种子将是当前时间,因此您很可能使用相同的种子启动多个PRNG,从而多次接收相同的随机值。请改用一个Random - 实例:

private Random rnd = new Random();

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

This应该有助于更好地理解这个问题。

一般实施

尝试尽可能贴近给定的任务。例如。问题明确指出您应该使用变量来指定数组的长度,同时您使用固定的魔术数字。该任务明确声明使用两个数组fizzbuzz,您使用一个名为fizzbuzz的数组。

GetFizzBuff内,您可能需要x = 2;而不是x = x + 2;。除此之外这部分还可以。

输出

如上所述:尽量坚持给定的任务。它明确声明只输出嘶嘶声和嗡嗡声,而不是任何整数。或者至少尝试在同一行中打印一个值和相应的fizzes和buzz,以产生可读的输出。

相关问题