java - 如何编写特定的随机数方案?
问题描述
我正在学习Java。我只想写一个生成特定集合中随机数的方法,比如银行信用卡“xxxx - xxxx - xxxx - xxxx”。每个块的长度必须正好为 4 位。有人有什么解决办法吗?
public class Main {
公共静态用户 accountHolders[] = new User[100]; 公共静态int索引= 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("\t-----\tWelcome to The Bank portal\t-----");
System.out.println("\nPlease enter the number of your required action");
System.out.println("\n1) Make a Deposit");
System.out.println("2) Get balance");
System.out.println("3) Register new account");
System.out.println("0) Quit the portal");
int command = input.nextInt();
switch (command) {
case 1:
InputController.deposit();
break;
case 2:
InputController.balance();
break;
case 3:
User user = InputController.register();
accountHolders[index] = user;
index++;
break;
default:
System.out.println("Wrong input! please enter a number between 1 - 4!!!");
break;
}
}
}
}
public class Account {
private int uniID;
private int password;
private int balance;
private String cardNum;
public int getUniID() {
return uniID;
}
public int getPassword() {
return password;
}
public int getBalance() {
return balance;
}
public void makeDeposit(int amount) {
this.balance = this.balance + amount;
}
public Account(int password, int balance) {
this.password = password;
this.balance = balance;
Random rand = new Random();
this.uniID = rand.nextInt(100);
}
}
public class User {
private String name;
private Account account;
public String getName() {
return name;
}
public Account getAccount() {
return account;
}
public User(String name, Account account) {
this.name = name;
this.account = account;
}
}
public class InputController {
static Scanner input = new Scanner(System.in);
public static User register() {
System.out.print("Enter your name: ");
String name = input.next();
System.out.print("Enter your password: ");
int password = input.nextInt();
System.out.print("Please enter the amount of your initial deposit: ");
int balance = input.nextInt();
Account account = new Account(password, balance);
User user = new User(name, account);
System.out.print("Your account number is: " + account.getUniID());
return user;
}
public static void balance() {
int index = findAccount();
if (index != -1) {
System.out.print("Enter your password: ");
int password = input.nextInt();
if (Main.accountHolders[index].getAccount().getPassword() == password) {
System.out.println("Your balance is: " +
Main.accountHolders[index].getAccount().getBalance());
} else {
System.out.println("Wrong password");
}
}
}
public static void deposit() {
int index = findAccount();
if (index != -1) {
System.out.print("Enter the required amount: ");
int money = input.nextInt();
Main.accountHolders[index].getAccount().makeDeposit(money);
System.out.println("Deposit successful");
}
}
private static int findAccount() {
System.out.print("Please Enter your account number: ");
int accountNum = input.nextInt();
for (int i = 0; i < Main.index; i++) {
User user = Main.accountHolders[i];
int id = user.getAccount().getUniID();
if (id == accountNum) {
return i;
} else if (id != accountNum){
System.out.println("Account number not found");
}
}
return -1;
}
}
抱歉,这有点长,但现在这就是全部。我想在每次注册新帐户时为信用卡号用户引入一个新变量。
解决方案
你可以试试下面的代码吗?如果你只想要一次,只需删除 for 循环。
public class RandomSetGenerator {
public static void main(String[] args) {
Random generator = new Random();
for (int i = 0; i < 10; i++) {
String string = Integer.toString(generator.nextInt(9000) + 1000) + "-" + Integer.toString(generator.nextInt(9000) + 1000) + "-" + Integer.toString(generator.nextInt(9000) + 1000) + "-" + Integer.toString(generator.nextInt(9000) + 1000);
System.out.println("Specific set of random number generated :" + string);
}
}}
推荐阅读
- python - 将不同 Python 模块中的函数导入 Apache Spark
- scheme - 在球拍方案或其他方案中解码十六进制格式字符串的惯用方法
- docker - 无法在 docker 图像中看到图像名称和标签
- azure - 将数据插入 cosmosdb 时,有没有办法增加请求大小限制?
- docker-compose - Kong:docker-compose [PostgreSQL 错误] 无法检索 PostgreSQL server_version_num:主机或服务未提供,或未知
- sql - 即使在使用 to_date(to_char(JOB_CLOSED_DATE,'dd-mon-yy')) 之后,日期列仍显示重复记录,
- c# - 用于在 Build Pipeline VSTS 中添加 timeoutinminutes 的 Yaml 脚本
- ruby-on-rails - Rails 渲染 HTML 也会在相同的操作中下载 CSV 文件
- html - 保存/恢复会影响 HTML5 画布中的 clip() 吗?
- rxjs - RxJS 在属性中的对象数组上不同