当前位置:  开发笔记 > 编程语言 > 正文

将参数输入到对象数组中?

如何解决《将参数输入到对象数组中?》经验,为你挑选了1个好方法。

我试图在一个对象数组中输入值,但我似乎无法让它工作.从本质上讲,我只需要创建一系列可以提取和存款的帐户.我试着去找我的教授寻求帮助,她说我只需要初始化值,因为它们最初都是null.但是,我不确定这个的语法,我的书有类似的方法,但不完全一样.任何人都可以帮我这个吗?

import java.util.Scanner;
import java.text.NumberFormat;
public class AccountMain
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    Account[] acct = new Account[30];
    for (int count = 0; count < 30; count++)
    {
      acct[count] = new Account("", 0, 0);
    }
                                                                                //creates accounts
    acct[0].Account("Jerry Longhorn", 1, 50.50);
    acct[1].Account("Jimmy Longhorn", 2, 700.20);
    acct[2].Account("Jenny Longhorn", 3, 10000.50);
    acct[3].Account("Jeffrey Longhorn", 4, 400.20);
    acct[4].Account("Jack Longhorn", 5, 500.20);

                                                                                //Determines withdraw or deposit
    System.out.println("Please enter your account number: ");
    int num = scan.nextInt();
    System.out.println("Enter W for withdrawl; D for deposit: ");
    char choice = scan.next().charAt(0);

    if (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd')
    {

      if (choice == 'W' || choice == 'w')
      {
        System.out.println("Enter amount to withdraw: ");
        double WithdrawMoney = scan.nextDouble();
        acct[--num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
        System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$");
        System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");
      }
      if (choice == 'D' || choice == 'd')
      {
        System.out.println("Enter amount to deposit: ");
        double DepositMoney = scan.nextDouble();
        acct[--num].deposite(DepositMoney);                                    //MUST --num in order to account for 0
        System.out.println("User # " + num + " funds after deposit: " + acct[--num].getBalance() + "$");
        System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");
      }
      else
        System.out.println("Invalid input.");



    }
  }
}

支持班:

import java.text.NumberFormat; //links to Part2

public class Account
{
  private final double RATE = 0.03; //Interest is 3%

  private int acctNumber;
  private String name;
  private double balance;

  //Defines owner, account number, and initial balance.
  public Account(String owner, int account, double initial)
  {
    name = owner;
    acctNumber = account;
    balance = initial;
  }

  //deposits a specified amount and returns new balance
  public double deposit(double amount)
  {
    balance = balance + amount;
    return balance;
  }

  //withdraws the specified amount from the account and applies the fee
  //                                                  + returns balance
  public double withdraw(double amount)
  {
    int fee = 1;
    balance = balance - amount - fee;
    return balance;
  }

  //Adds interest to the account
  public double addInterest()
  {
    balance += (balance * RATE);
    return balance;
  }
  public double getBalance()
  {
    return balance;
  }

  //returns a one line description of the account as a string
  public String toString()
  {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    return acctNumber + "/t" + name + "/t" + fmt.format(balance);
  }
}

Stop ongoing.. 5

这是无效的语法:

acct[0].Account("Jerry Longhorn", 1, 50.50);
acct[1].Account("Jimmy Longhorn", 2, 700.20);
acct[2].Account("Jenny Longhorn", 3, 10000.50);
acct[3].Account("Jeffrey Longhorn", 4, 400.20);
acct[4].Account("Jack Longhorn", 5, 500.20);

你可能意味着:

acct[0] = new Account("Jerry Longhorn", 1, 50.50);
acct[1] = new Account("Jimmy Longhorn", 2, 700.20);
acct[2] = new Account("Jenny Longhorn", 3, 10000.50);
acct[3] = new Account("Jeffrey Longhorn", 4, 400.20);
acct[4] = new Account("Jack Longhorn", 5, 500.20);

顺便说一下,你在这里也有一个语法错误:

acct[--num].deposite(DepositMoney);                                    

看起来像一个简单的拼写错误:

acct[--num].deposit(DepositMoney);                                    

而且......有很多问题....例如:

acct[--num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$");
System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");

重复--num索引在这里发生了什么?我严重怀疑你真的想减少那里的索引变量.我怀疑这更接近你真正想要的(num而不是所有--num):

acct[num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
System.out.println("User # " + num + " funds after withdraw: " + acct[num].getBalance() + "$");
System.out.println("User # " + num + " funds after interest: " + acct[num].addInterest() + "$");

无论如何,这有点难以辨别.代码有几个问题,通过问题 - 答案调试是不太实际的.



1> Stop ongoing..:

这是无效的语法:

acct[0].Account("Jerry Longhorn", 1, 50.50);
acct[1].Account("Jimmy Longhorn", 2, 700.20);
acct[2].Account("Jenny Longhorn", 3, 10000.50);
acct[3].Account("Jeffrey Longhorn", 4, 400.20);
acct[4].Account("Jack Longhorn", 5, 500.20);

你可能意味着:

acct[0] = new Account("Jerry Longhorn", 1, 50.50);
acct[1] = new Account("Jimmy Longhorn", 2, 700.20);
acct[2] = new Account("Jenny Longhorn", 3, 10000.50);
acct[3] = new Account("Jeffrey Longhorn", 4, 400.20);
acct[4] = new Account("Jack Longhorn", 5, 500.20);

顺便说一下,你在这里也有一个语法错误:

acct[--num].deposite(DepositMoney);                                    

看起来像一个简单的拼写错误:

acct[--num].deposit(DepositMoney);                                    

而且......有很多问题....例如:

acct[--num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$");
System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");

重复--num索引在这里发生了什么?我严重怀疑你真的想减少那里的索引变量.我怀疑这更接近你真正想要的(num而不是所有--num):

acct[num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
System.out.println("User # " + num + " funds after withdraw: " + acct[num].getBalance() + "$");
System.out.println("User # " + num + " funds after interest: " + acct[num].addInterest() + "$");

无论如何,这有点难以辨别.代码有几个问题,通过问题 - 答案调试是不太实际的.

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有