gpt4 book ai didi

java - 如何编写特定的随机数方案?

转载 作者:行者123 更新时间:2023-12-02 09:04:10 26 4
gpt4 key购买 nike

我正在学习Java。我只想编写一个方法来生成特定集合中的随机数,例如银行信用卡“xxxx - xxxx - xxxx - xxxx ”。每个 block 的长度必须恰好为 4 位数字。有人有解决办法吗?

public class Main {

public static User accountHolders[] = new User[100]; 公共(public)静态 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);
}
}}

关于java - 如何编写特定的随机数方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59960686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com