【问题标题】:Why does my method overwrite positions in my array为什么我的方法会覆盖数组中的位置
【发布时间】:2020-02-19 19:11:20
【问题描述】:

所以我这里有这个方法

    while(oMenu == 1 || oMenu == 2){
            oMeny = Kbd.readInt("\nClick 1 to make an account\nClick 2 to login\nClick 3 to exit the program");
            if(oMeny == 1){
                for(int i = 0; Account[i] != null; i++){
                    if(Account[i] == null){
                        pos = i;
                    }
                }

                Account[pos] = new Account();


            }

            if(oMeny == 2){
                String s = Kbd.readString("Input your accountnumber: ");
                for(int i = 0; Account[i] != null; i++){
                    if(Account[i] != null && s.equals(Account[i].getAccountNumber())){
                        System.out.println("Welcome!");
                         // Here is rest of my code , the "inner" menu that works menyMetod(iMeny,mMeny);
                    }
                    else{
                        System.out.println("There are no accounts with that given accountnumber!");  
                    }
                }                
            }

        }
    } 

我想了解为什么如果我访问 oMeny == 1 并创建 2 个帐户 为什么我似乎无法访问我创建的第一个帐户,而是访问最新的帐户?似乎我的数组以某种方式“覆盖”了第一个空位置。基本上我想在我的数组中找到第一个空位置,所以在第一种情况下它总是索引 0,然后下次我再次创建帐户时,它应该是逻辑上的索引 1。

编辑:这是我的 Account 类代码

public class Account{

private int money, transactions;
private String AccountNumber;

public Account(){
    money = Kbd.readInt("\nHow much money do you want to put in?");
    AccountNumber = Kbd.readString("\nWhat account number do you want?");
}

【问题讨论】:

  • 请注意:您的第一个 if 语句引用了一个名为“oMenu”的变量,您是说它是“oMeny”吗?
  • 是的,应该是,我将变量名翻译成英文所以可能错过了那部分。
  • 创建new Account() 时,accountNumber 未设置,因此在搜索时永远不会匹配。此外,数组不是动态大小的 - 你可以使用列表吗?或者使用键为 accountNumber 的 Map 可能更好。
  • 哦,我应该提一下,在我设置的 Account 类中,您输入 Account number 这里是 account 类的代码 编辑:我将把代码放在帖子的新编辑中
  • 目前在我的 Java 类中,除了数组之外,我们还没有使用任何其他数据结构。所以我知道我可以使用地图或列表,但最好不要使用它们

标签: java arrays while-loop logic account


【解决方案1】:

错误在这里:

for (int i = 0; accounts[i] != null; i++) {
     if (accounts[i] == null)

只要 i 指向一个非空条目,for 循环就会重复。因此,if 条件永远不会为真。

当您在调试器中逐行运行程序时,这一点很快就会变得明显。

下次请提供可以编译的完整代码示例。您的代码充满了错误,我花了很多时间来修复它,然后才能执行它。

更正的代码:

import java.util.Scanner;

class Main
{
    static Scanner kbd = new Scanner(System.in);
    static Account[] accounts = new Account[100];

    static class Account
    {
        //public int money;
        public String accountNumber;

        public Account()
        {
            //System.out.println("\nHow much money do you want to put in?");
            //money = Kbd.nextInt();
            System.out.println("\nWhat account number do you want?");
            accountNumber = kbd.next();
        }
    }

    public static void main(String[] args)
    {
        int oMenu = 1;
        int pos = 0;
        while (oMenu == 1 || oMenu == 2)
        {
            System.out.println("\nClick 1 to make an account\nClick 2 to login\nClick 3 to exit the program");
            oMenu = kbd.nextInt();
            if (oMenu == 1)
            {
                for (int i = 0; i<accounts.length; i++)
                {
                    if (accounts[i] == null)
                    {
                        accounts[i] = new Account();
                        break;
                    }
                }
            }

            if (oMenu == 2)
            {
                System.out.println("Input your accountnumber: ");
                String s = kbd.next();
                Account found=null;
                for (int i = 0; i<accounts.length; i++)
                {
                    if (accounts[i] != null && s.equals(accounts[i].accountNumber))
                    {
                        found=accounts[i];
                    }
                }
                if (found!=null)
                {
                    System.out.println("Welcome! nr. "+found.accountNumber);
                }
                else
                {
                    System.out.println("There are no accounts with that given accountnumber!");
                }
            }
        }
    }
}

注意我是如何修复第二个 for 循环的。

【讨论】:

  • 谢谢!我现在意识到了这个错误。但是非常抱歉我没有提供所有代码,下次会尽力做到最好。然而,我的 Java 老师写了那个 for 循环,所以我没想到这是错误的。再次感谢您!
【解决方案2】:

您没有显示 pos 的声明或初始化,所以我认为它没有按您的预期工作,因为您没有进入 Account[i] 为 null 的 for 循环来设置 pos。试试这个

 if(oMenu == 1){
    int pos = 0;
    while (Account[pos] != null && pos < Account.length)
        pos++;
    if (pos < Account.length)
        Account[pos] = new Account();
    else{
         //expand array and add account or throw error
    }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2017-08-23
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2015-04-05
    • 2021-04-13
    相关资源
    最近更新 更多