간단한 은행 계좌 활동을 시뮬레이트하는 프로그램을 작성 중이며 매개 변수없이 새 계정을 만들면 임의의 7 자리 숫자로 표시된 ID 번호를받는 방법이 궁금합니다. 끈. 내가하는 것처럼 출력에서 [email protected] 만 수신합니다. 이 웹 사이트에 게시 한 첫 번째 질문이므로이 질문에 대한 도움이나 의견이 있으리라 생각합니다.임의의 int 값이있는 필드
import java.util.Random;
class Account {
String id;
double stan;
int num;
static int counter;
public Account() {
**id = randomId().toString();**
stan = 0;
num = ++counter;
}
public Account(String id) {
this.id = id;
stan = 0;
num = ++counter;
}
public Account(String id, double mon) {
stan = 0;
this.id = id;
this.stan = mon;
num = ++counter;
}
**static String randomId() {
Random rand = new Random(7);
return String.valueOf(rand);**
}
String getId() {
return id;
}
double getStan() {
return stan;
}
int getNum() {
return num;
}
@Override
public String toString() {
return "Account's id " + getId() + " and balance " + getStan();
}
}
public class Exc7 {
public static void main(String[] args) {
Account account = new Account("0000001"),
acount0 = new Account("0000002", 1000),
acount1 = new Account();
System.out.println(account + "\n" + account0 + "\n" + account1);
}
}
첫째, 감사합니다. 둘째, 나는 실수를해서 7을 무작위로 입력하면 7 자리 숫자가 나에게 줄 것이라고 가정합니다. 이 범위의 숫자를 받거나 다른 방식으로 사용하려면 9999999를 넣어야합니까? – Konrad
@Konrad 당신을 환영합니다! 솔직히 말해서 나는'Random' 생성자에 의해 산만하고 속임을당했습니다 : 실제로 그것은 자릿수를 정의하는 것이 아니라 무작위 적 시드를 정의합니다. 특정 자릿수를 [here] (http://stackoverflow.com/questions/5392693/java-random-number-with-given-length) 시작할 수 있습니다. 그렇지 않으면'String'을 자릅니다. – Mena