我只是制作超市程序并使用像这样的对象数组从类中初始化对象
swalayanTinyV2 sw[] = new swalayanTinyV2[3];
sw[0] = new swalayanTinyV2("Kevin", "3812345678", 5000000, 240898 );
sw[1] = new swalayanTinyV2("Sandra", "5612345678", 6000000, 240899);
sw[2] = new swalayanTinyV2("Ryan", "7612345678", 7000000, 240897);
然后我进行迭代以找到与客户名称和PIN类似的对象。
int limit = 3;
for (int swalayanTinyV2 sw1 : sw) {
while (limit!=0) {
System.out.print("username : ");
String username = in.nextLine();
System.out.print("PIN : ");
int pin = in.nextInt();
in.nextLine();
if (username.equalsIgnoreCase(sw1.getCustName()) && pin==sw1.getPin()) {
sw1.displayMessage();
System.out.println("MENU : ");
System.out.println("");
System.out.println("1. Buy Goods");
System.out.println("2. Top Up");
System.out.print("Input your choice : ");
int pil = in.nextInt();
System.out.println("");
switch (pil) {
case 1 :
System.out.print("Total of price : ");
double total = in.nextDouble();
sw1.setTotalBelanja(total);
sw1.getTotalBelanja();
sw1.Struk();
break;
case 2 :
System.out.print("Add value : ");
double topup = in.nextDouble();
System.out.println("=============================================================");
System.out.println("Value added : Rp. "+topup);
System.out.println("Your value : Rp. "+sw1.getTopUpSaldo(topup));
break;
default : System.out.println("Invalid input!");
}
return;
}
else {
System.out.println("Incorrect username/PIN!");
limit--;
System.out.println("Your chance to login : "+limit);
}
}
if (limit==0) {
System.out.println("Account has been banned.");
}
}
但是迭代使程序只引用了数组对象的第一个索引。 (当我在数组对象的第二个索引中输入用户名和PIN时,它称为无效的用户名/ PIN)。
请帮忙。