Java 不能进入循环
目标是为ATM对象创建帐户注册方法。但我一直在破坏用于进入循环的if语句。我想我的“措辞”是错误的,但我在如何解决这个问题上还是一片空白。有什么建议吗?问题本身在Java 不能进入循环,java,netbeans,Java,Netbeans,目标是为ATM对象创建帐户注册方法。但我一直在破坏用于进入循环的if语句。我想我的“措辞”是错误的,但我在如何解决这个问题上还是一片空白。有什么建议吗?问题本身在if(acc[i].getAcc()==0)中,其中。getAcc是类中的getter package atmassignment; import java.util.Scanner; public class AtmAssignment { static Scanner in = new Scanner(System.in); st
if(acc[i].getAcc()==0)
中,其中。getAcc
是类中的getter
package atmassignment;
import java.util.Scanner;
public class AtmAssignment {
static Scanner in = new Scanner(System.in);
static int cou = 1;
static Account[] acc = new Account[10];
public static void main(String[] args) {
menu();
}
public static void menu(){
char opt;
System.out.println("Thanks for accessing ATManager.");
System.out.println("Please select a menu option to proceed.");
System.out.println("1-Register a new account, 2-Access an account, 0-Exit ATManager");
opt = in.next().charAt(0);
switch (opt) {
case '1':
newAccount();
break;
case '2':
selAccount();
break;
case '0':
System.out.println("Goodbye.");
break;
default:
System.out.println("Invalid Entry.");
break;
}
}
public static void newAccount(){
System.out.println("Account registration");
for (int i = 0; i < acc.length; i++){
if(acc[i].getAcc()==0){
System.out.println("Please enter your first name...");
String fn = in.next();
System.out.println("Please enter your last name...");
String ln = in.next();
System.out.println("Please enter your address...");
String ad = in.next();
System.out.println("Please provide a contact number...");
String cn = in.next();
Customer cus = new Customer(fn, ln,ad,cn);
System.out.println("What is your starting balance...");
double bal = in.nextDouble();
acc[i] = new Account(cou, bal, cus);
System.out.println("Your account is registered as ID#"+cou);
break;
} else {
System.out.println("Sorry, no more accounts can be created.");
break;
}
}
}
}
大规模包装;
导入java.util.Scanner;
公共课{
静态扫描仪输入=新扫描仪(系统输入);
静态int cou=1;
静态账户[]acc=新账户[10];
公共静态void main(字符串[]args){
菜单();
}
公共静态无效菜单(){
字符选择;
System.out.println(“感谢访问ATManager”);
System.out.println(“请选择一个菜单选项继续”);
System.out.println(“1-注册新帐户,2-访问帐户,0-退出ATManager”);
opt=in.next().charAt(0);
开关(opt){
案例“1”:
newAccount();
打破
案例“2”:
selcount();
打破
案例“0”:
System.out.println(“再见”);
打破
违约:
System.out.println(“无效条目”);
打破
}
}
公共静态无效newAccount(){
System.out.println(“账户注册”);
对于(int i=0;i
这将分配一个对象引用数组:
static Account[] acc = new Account[10];
然而,它实际上并没有分配任何对象,所以当您尝试访问第一个元素时,可能会出现空指针异常。在init代码中,执行如下操作:
for(int i = 0; i < 10; i++)
acc[i] = new Account();
for(int i=0;i<10;i++)
acc[i]=新账户();
在实际构建对象之前调用acc[i].getAcc()==0
。也许可以修改for循环,以便创建对象,然后收集输入并稍后使用setter方法更新对象?这当然需要为Account类提供某种默认构造函数
acc[j through maxLength] = new Account(); //where j spans the entire length of the array
for (int i = 0; i < acc.length; i++){
if(acc[i].getAcc()==0){
System.out.println("Please enter your first name...");
String fn = in.next();
System.out.println("Please enter your last name...");
String ln = in.next();
System.out.println("Please enter your address...");
String ad = in.next();
System.out.println("Please provide a contact number...");
String cn = in.next();
Customer cus = new Customer(fn, ln,ad,cn);
System.out.println("What is your starting balance...");
double bal = in.nextDouble();
acc[i].setContact(###);
acc[i].setBalance(###); //ETC
System.out.println("Your account is registered as ID#"+cou);
break;
} else {
System.out.println("Sorry, no more accounts can be created.");
break;
}
}
acc[j到maxLength]=新帐户()//其中j跨越阵列的整个长度
对于(int i=0;i
可能您没有填充您的帐户[]数组。在这里我只能看到你像这样声明你的数组-
static Account[] acc = new Account[10];
所以,在这之后,当您试图让您的数组元素使用for循环时,您将无法访问它们。您在if(acc[i].getAcc()==0)
这一行得到错误。因此,我建议删除if检查,因为您稍后将在此行中填充您的帐户-acc[I]=新帐户(cou、bal、cus)代码>
希望能有所帮助。
非常感谢 正如samgak所建议的,检查getAcc方法返回的int值是否被初始化!