これは私の宿題です:
Random r = new Random(); public int get100RandomNumber() { return 1 + r.nextInt(100); }
getrand100() (上記) という名前の定義済み関数が与えられます。この関数は、1 から 100 までの乱数である整数を返します。この関数は何度でも呼び出すことができますが、この関数はリソースを大量に消費することに注意してください。他の乱数発生器は使用できません。の定義を変更することはできません
getrand100()
。出力: 1 ~ 20 の数字をランダムな順序で出力します。(20個の乱数ではありません)
私が試したこと..
public class MyClass {
static Random r = new Random();
static HashSet<Integer>;
public static void main(String args[]) {
myMethod();
System.out.println(s);
}
public static void myMethod() {
boolean b = false;
s = new HashSet<Integer>();
int i = getRand100();
if (i >= 20)
i = i % 20;
int j = 0;
int k, l;
while (s.size() <= 20)
{
System.out.println("occurence no" + ++j);
System.out.println("occurence value" + i);
b = s.add(i);
while (!b) {
k = ++i;
if(k<=20)
b = s.add(k);
if(b==true)
break;
if (!b) {
l = --i;
if(i>=1&&i<=20)
b = s.add(l);
if(b==true)
break;
}
}
}
System.out.println(s);
}
public static int getRand100()
{
return r.nextInt(100) + 1;
}
}
助けてくれてありがとう!