为什么我的方法会覆盖数组中的位置
Why does my method overwrite positions in my array
所以我这里有这个方法
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 个帐户为什么我似乎无法访问我创建的第一个帐户而是最新的帐户?似乎不知何故我的数组 "overwrites" 第一个空位置。基本上我想在我的数组中找到第一个空位置,所以在第一种情况下它总是索引 0 然后下次我再次创建帐户时,逻辑上它应该是索引 1。
编辑:这是我的帐户代码 class
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?");
}
您没有显示 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
}
}
错误在这里:
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 循环。
所以我这里有这个方法
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 个帐户为什么我似乎无法访问我创建的第一个帐户而是最新的帐户?似乎不知何故我的数组 "overwrites" 第一个空位置。基本上我想在我的数组中找到第一个空位置,所以在第一种情况下它总是索引 0 然后下次我再次创建帐户时,逻辑上它应该是索引 1。
编辑:这是我的帐户代码 class
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?");
}
您没有显示 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
}
}
错误在这里:
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 循环。