icecream.java

来自「think in java TIJ-3rd-edition-code.zip」· Java 代码 · 共 46 行

JAVA
46
字号
//: c09:IceCream.java
// Returning arrays from methods.
package c09;
import com.bruceeckel.simpletest.*;

public class IceCream {
  static String[] flav = {
    "Chocolate", "Strawberry",
    "Vanilla Fudge Swirl", "Mint Chip",
    "Mocha Almond Fudge", "Rum Raisin",
    "Praline Cream", "Mud Pie"
  };
  static String[] flavorSet(int n) {
    // Force it to be positive & within bounds:
    n = Math.abs(n) % (flav.length + 1);
    String[] results = new String[n];
    boolean[] picked =
      new boolean[flav.length];
    for (int i = 0; i < n; i++) {
      int t;
      do
        t = (int)(Math.random() * flav.length);
      while (picked[t]);
      results[i] = flav[t];
      picked[t] = true;
    }
    return results;
  }
  public static void main(String[] args) {
    SimpleTest monitor =
      new SimpleTest("IceCream");
    for(int i = 0; i < 20; i++) {
      System.out.println(
        "flavorSet(" + i + ") = ");
      String[] fl = flavorSet(flav.length);
      for(int j = 0; j < fl.length; j++)
        System.out.println("\t" + fl[j]);
      monitor.expectRegularExpression(new Object[] {
        new RegularExpression("flavorSet\\(\\d{1,2}\\) = ", 1),
        new RegularExpression("\\t(Chocolate|" +
          "Strawberry|Vanilla Fudge Swirl|" +
          "Mint Chip|Mocha Almond Fudge|" +
          "Rum Raisin|Praline Cream|Mud Pie)", 8)});
    }
  }
} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?