user9821567:
public static void setIdentificationNumber2(Staff a) {
if ("Librarian".equals(a.getPosition())) {
a.setIdentificationNumber(4 + (int) (Math.random() * 999999 + 1));
} else {
a.setIdentificationNumber(3 + (int) (Math.random() * 999999 + 1));
}
}
形式3XXXXXXで、このメソッドから生成された任意のランダムな番号の先頭に3を追加する方法があった場合だけ思っていました。すべてのヘルプは大歓迎です!
リノ:
最も簡単な解決策は、あなたの乱数から文字列を作成、追加することで"3"
、フロントには、その後に戻ってそれを解析しますint
:
int randomNumber = (int) (Math.random() * 999999 + 1);
int identificationNumber = Integer.parseInt("3" + Integer.toString(randomNumber));
a.setIdentificationNumber(identificationNumber);
あなたの番号は必ず7桁の長さでなければならない場合は、その後、あなただけ追加することができ3000000
、それに:
int randomNumber = (int) (Math.random() * 999999 + 1);
int identificationNumber = 3_000_000 + randomNumber;
a.setIdentificationNumber(identificationNumber);