繁体   English   中英

使用Java中的扫描器将对象添加到阵列列表

[英]Adding objects to an array list with scanner in Java

我有经典的“银行帐户”分配问题。 我已经学会了如何将对象添加到列表中,但是我不知道如何通过用户输入来实现。 我找不到我要找的东西。

我想制作一个名为enterDetails的方法,该方法将提示用户输入“名称”作为字符串,并输入“金额”作为双enterDetails 然后,这需要创建一个对象,该对象存储在ArrayList<Account>accounts = new ArrayList<>(); 需要重复该步骤,直到用户输入q退出为止。

到目前为止,这是我的代码,不确定我是否朝着正确的方向前进。

class Bank
{
    ArrayList<Account>accounts = new ArrayList<>();

    public static void enterDetails()
    {
        int amount = Scanner.nextInt();    

        for (int i = 0; i < amount ; i++) {
            System.out.println("ENTER NAME");
            Scanner addName = new Scanner(System.in);
            String name = (addName.nextLine());

            System.out.println("Enter Current balance");
            Scanner addBalance = new Scanner(System.in);
            double balance = (addBalance.nextDouble());
        }

    }
}
package com.stackOverflow.practice.banking;

public class Account {
      private String name;
      private double amount;

      public Account(String name, double amount) {
           this.name = name;
           this.amount = amount;

      }

      public double getAmount() {
           return this.amount;
      }

      public String getName() {
          return this.name;
      }

 }

在创建Account类以创建Account数据类型之后,创建Bank类:

package com.stackOverflow.practice.banking;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.Collator;


public class App {

        public static void main(String... args) throws IOException, MyException{

        ArrayList<Account> ac = new ArrayList<Account>();
        Scanner scan = new Scanner(System.in);
        boolean isRunning = true;

        while(isRunning){
              System.out.println("Please enter your name in the account(quit to exit program): ");
              String name = scan.next();
              if(name.equalsIgnoreCase("quit")) {
                  break;
              }
              System.out.println("Please enter an amount for your account: ");
              double amount = scan.nextDouble();
              Account a = new Account(name, amount);
              ac.add(a);

         }

         for(Account t: ac) {
              System.out.println("Name: " + t.getName() + "\nAmount: " + t.getAmount());
              System.out.println();
         }

    }
}

这是输出:

Please enter your name in the account(quit to exit program): 
Simeon
Please enter an amount for your account: 
200.00
Please enter your name in the account(quit to exit program): 
Henry
Please enter an amount for your account: 
100.00
Please enter your name in the account(quit to exit program): 
Harry
Please enter an amount for your account: 
13.99
Please enter your name in the account(quit to exit program): 
quit
Name: Simeon
Amount: 200.0

Name: Henry
Amount: 100.0

Name: Harry
Amount: 13.99

在这里,我们仅使用私有字段名称和金额创建帐户类。 如您所述,名称必须为String,数量必须为double,因此我们将其设置为这些数据类型。 然后,我们创建一个构造函数以初始化这些私有字段,并在创建类的实例时为其赋予一个值。 我们添加名称和数量作为参数。 这是用户将值传递给构造函数的位置,以便为实例字段提供值。 然后,我们创建一个getName()和getAmount()方法,以测试我们的对象是否正常工作:

在主代码中,我们创建一个Arraylist <>,用于存储数据类型“ Account”。 然后,我们创建一个Scanner对象,该对象将存储用户输入。 isRunning将是用于控制while循环的标志,但是在这种情况下,我们可以简单地说while(true),因为我们使用break语句而不是将isRunning设置为false。 我只是想创建一个布尔表达式来阐明代码,但是您可以很容易地说出while(true)。 我们会提示用户输入他们的姓名。 我们还创建了一个条件语句,如果他们输入quit(或q,因为您表示希望用户输入q来退出),则该条件语句将跳出循环。 如果用户未输入quit,我们会将其名称存储在String变量中。 然后,我们提示用户输入双倍金额。 我们将其存储在变量“金额”中。 获得这些值后,我们将创建一个Account Object的新实例,并将用户输入的值传递给构造函数。 然后,我们使用Arraylist.add(Object)方法将对象存储在Arraylist中。

当最终退出循环时,我们运行一个for-each循环,以确保将对象正确存储到Arraylist中。 我们在Arraylist中的每个对象上显式调用getName()和getAmount()方法。 从打印的输出中可以看到,对象已正确存储。

警告,这只是伪代码,其中包含有关将对象存储到Arraylist的解释。 这不是详尽的解释,您将必须根据自己的喜好修改代码。 这是一个广泛而简单的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM