这是我的计划流程的一个例子:
Enter an issuer ID# (6 digits): 12345
java.io.IOException: Invalid issuer ID
Enter an issuer ID# (6 digits): 987
java.io.IOException: Invalid issuer ID
Enter an issuer ID# (6 digits): 555
java.io.IOException: Invalid issuer ID
Enter an issuer ID# (6 digits): 456456
这是我的代码:
public static ArrayList<HW6Re_doHW4> getarray() throws IOException
{
int size;
ArrayList<HW6Re_doHW4> card;
String id;
HW6Re_doHW4 temp = new HW6Re_doHW4();
boolean j = false;
System.out.printf("Enter the number of elements in the array: ");
size = scanner.nextInt();
if(size < 1)
{
size = 1;
}
card = new ArrayList<HW6Re_doHW4>(size);
while(j == false)
{
try
{
System.out.printf("Enter an issuer ID# (6 digits): ");
id = scanner.next();
for(int i = 0; i < card.size(); i++)
{
temp.createcard(id);
card.add(temp);
}
j = true;
}
catch(IOException e)
{
System.out.println(e);
j = false;
}
}
return card;
}
ID#应该再次询问,但我的输出才停止。我不知道自己哪里出错了。
输入数组中的元素数:0
输入发行人ID#(6位数):12345
答案 0 :(得分:0)
你认为会发生什么......
你有:
while(j==false) {
System.out.printf("Enter an issuer ID# (6 digits): ");
id = scanner.next();
for(int i = 0; i < card.size(); i++)
{
temp.createcard(id);
card.add(temp);
}
j = true;
}
因此,只要您要求提供ID并将其录入,就将j设置为true。因为你while
循环的条件是j必须是假的...
摆脱j = true;
。
答案 1 :(得分:0)
您似乎已将两个循环混为一谈,而您只需要一个0
到size
的循环。像
for(int i = 0; i < size; i++) { // <-- card.size() starts at 0.
try {
System.out.printf("Enter an issuer ID# (6 digits): ");
id = scanner.next();
// temp.createcard(id); // <-- probably not.
card.add(new HW6Re_doHW4(id)); // <-- something like.
} catch (IOException e) {
e.printStackTrace();
i--;
}
}