📄 icecream.java
字号:
//: 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -