将随机数提供给单独的类中的对象

时间:2016-12-09 04:24:51

标签: java arrays class object

我创建了一个程序,创建了10个基本帐户,其ID由4位数组成,默认余额为100.我正在尝试创建更多限制/附加信息的其他帐户(支票帐户)。为了访问帐户,您必须输入您的ID,该ID显示在所有可能ID的列表中(以避免猜测ID)。我现在的问题是我用我最初的10个标准帐户对象创建了10个Checkingaccount对象我的程序不会为这两个对象分配一个随机的四位数ID。对象帐户获得ID很好但是如果你运行该程序,那么Checkingaccount对象就不会收到ID' s。为什么?

我已经尝试了好几个小时,我对此非常陌生,所以请原谅我,如果这在某种程度上是破碎或不正确的。我非常感谢你花时间给我帮助!

import java.util.Scanner;
import java.util.Random;
public class Chapter10_7  {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 

    CheckingAccount[] Checkingaccount = new CheckingAccount[10];//create ten CHECKING ACCOUNT objects
    account[] accounts = new account[10];//create ten accounts objects

    for (int i = 0; i < accounts.length; i++) {
        Random r = new Random();
        int ID = +((int)(Math.random()*9000)+1000);//ID is 4 digit number
        accounts[i] = new account(ID, 100); //assign random 4 digit ID to accounts[1-10]
        System.out.println(ID);
    }
    System.out.println("\n--Checking Accounts--\n");
    for (int i = 0; i < Checkingaccount.length; i++) {
        Random r = new Random();
        int ID = +((int)(Math.random()*9000)+1000);//ID is 4 digit number
        Checkingaccount[i] = new CheckingAccount(ID, 100); //assign random 4 digit ID to CHECKING ACCOUNT[1-10]
        System.out.println(ID);
    }


boolean hasRan = false;
int q = 0;
int a = q;
int userID = 0;
int accountID = 0;
int CheckingaccountID = 0;
int z = 0;

while(z != 4){//run until user enters 4


    while(hasRan == false){//runs once and never again unless its incorrect ID
    System.out.println("Enter Account ID");
    userID = input.nextInt();

    for(a = 0; a < accounts.length; a++){
        accountID = accounts[a].getId(); //accountID gets accounts[a] ID variable and stores it(4 digit ID).getId
        CheckingaccountID = Checkingaccount[a].getId();
        if(userID == accountID){//if your input of ID equals an accounts pin(any of them) it allows access
            System.out.println("Welcome");
            hasRan = true;//stops it from looping 
            break;
        }if(userID == CheckingaccountID){//if your input of ID equals a Checking Account pin(any of them) it allows access
            System.out.println("Welcome");
            hasRan = true;//stops it from looping 
            break;
        }else if(userID != accountID){
            hasRan = true;//loop if you entered incorrect pin

        }

    }
}

if(userID == accountID){// if ID matches an existing accounts ID then access



System.out.println("\n1:Check Balance\n2:Deposit\n3:Withdraw\n4:Exit");
int x = input.nextInt();

//accounts[a] is the account array,1-10 since we have 10 accounts.

            if (x == 1){
            System.out.println("Your Balance Is: " + accounts[a].getBalance());
            accounts[a].getBalance();

            }else if (x == 2){
            System.out.println("How Much Would You Like To Deposit?");
            int newDeposit = input.nextInt();
            accounts[a].deposit(newDeposit);
            System.out.println("Your Total Is: "+ accounts[a].getBalance());

            }else if(x == 3){
                System.out.println("How Much Would You Like To Withdraw?");
                int newWithdraw = input.nextInt();
                accounts[a].withdraw(newWithdraw);
                System.out.println("Your Total Is: "+     accounts[a].getBalance());
                }else break;

            }else{
                hasRan = false;// allows ID check to run again
        }
    }
 }

 }

- 这是我创建的课程,只使用10个基本&#34;帐户&#34;对象。

class account{

private int id = 0;
private double balance = 100;

public account(int newId , double newBalance ){
      id = newId;
      balance = newBalance;

}

public double getBalance() {
    return balance;
}

public void setBalance(double newBalance) {
    balance = newBalance;
}

public int getId() {
    return id;
}

public void setId(int newId) {
    id = newId;
}


public double deposit(double newDeposit){
    balance = balance + newDeposit;
    return newDeposit;
}

public double withdraw(double newWithdraw){
    balance = balance - newWithdraw;
    return newWithdraw;
}


@Override
public String toString() {
    return "\r\n"+"Account ID: " + id + "\r\n" + "Balance: " + balance +  "\r\n" ;
}

}

- 我试图通过帐户

使支票帐户在单独的类中工作
import java.util.Random;

public class CheckingAccount  {

private double balance;
private int id;

public CheckingAccount(int newId , double newBalance){

}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public int getId() {

    return id;
}

public void setID(int newId){
    id = newId;

}

}

1 个答案:

答案 0 :(得分:0)

您的问题 帐户获取ID正常但如果您运行该程序,则Checkingaccount对象不会收到ID。为什么?

您可以像

一样创建对象
new CheckingAccount(ID, 100);

但你的构造函数是错误的,并没有保存传递的值

您应该将其更改为

public CheckingAccount(int newId , double newBalance){
    this.id = newId;
    balance = newBalance;
}

另外,为什么要实例化

Random r = new Random();

当你从不使用它时。