java - 使用计数器向数组添加值
问题描述
在过去的 1.5 个月里,我一直在学习 java。现在,讲师要求我们创建一个程序,该程序从用户那里获取姓名和电话号码(但在一种方法中),直到用户输入“E”。然后程序应该打印存储在所有数组中的所有信息。
该程序有一个主菜单,用户将输入“1”来创建一个帐户(姓名和电话号码),然后主菜单再次出现,用户创建另一个帐户等等......直到他选择另一个菜单中的选项或输入“E”以存在并打印所有帐户的摘要。
我的问题是我试图创建一个计数器作为对数组中每个帐户点的引用(数组中的索引);因此,每次用户输入名称和数字后,计数器加 1,数组索引加 1 并移动到下一个位置……但这不起作用。
代码没写完,停在选择1测试create account方法
public static void addDonor(String[] a1, String[] a2, char[] a3, int[] a4, int [] a5){
Scanner input = new Scanner(System.in);
System.out.print(" Enter the name (first and last):" + " ");
String name = input.nextLine();
System.out.print(" Enter Mobile No.:" + " ");
String phone = input.next();
if (phone.length() < 10 && phone.startsWith("0") == false){
while (true){
System.out.println("Wrong Mobile NO... try again!");
System.out.print(" Enter Mobile No.:" + " ");
phone = input.next();
if (phone.length() > 10 || phone.startsWith("0") == true)
break;
}
}
System.out.print(" Enter Blood Group Type (A, B or O):" + " ");
char blood = input.next().charAt(0);
while (blood != 'a' || blood != 'b' || blood != 'c'){
System.out.println("Wrong Blood Group Type... try again!");
System.out.println(" Enter Blood Group Type (A, B or O):" + " ");
blood = input.next().charAt(0);
if (blood == 'A' || blood == 'B' || blood == 'O')
break;
}
int counter = 0;
a1[counter] = name;
a2[counter] = phone;
a3[counter] = blood;
a4[counter] = 1;
a5[counter] = 1;
counter++;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] Name = new String[20];
String[] Mobile = new String[20];
char[] Blood_Gp = new char[20];
int[] Count_o_Donation = new int[20];
int[] Blood_Stock = new int[20];
while (true){
displayMainMenu();
readAndVerify();
String choice = readAndVerify();
switch (choice){
case "1":
addDonor(Name, Mobile, Blood_Gp, Count_o_Donation, Blood_Stock);
break;
}
if (choice.equals("e"))
break;
}
System.out.println(Name[0]);
System.out.println(Name[1]);
}
解决方案
问题是您正在 addDonor 方法中创建变量索引。因此,每次调用该方法时,都会创建一个值为 0 的新变量,这就是它不移动的原因。您应该在方法外部创建变量并将其作为参数传递。
像这样的东西:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] Name = new String[20];
String[] Mobile = new String[20];
char[] Blood_Gp = new char[20];
int[] Count_o_Donation = new int[20];
int[] Blood_Stock = new int[20];
int index = 0;
while (true){
displayMainMenu();
readAndVerify();
String choice = readAndVerify();
switch (choice){
case "1":
addDonor(Name, Mobile, Blood_Gp, Count_o_Donation, Blood_Stock, index);
index++
break;
}
if (choice.equals("e"))
break;
}
System.out.println(Name[0]);
System.out.println(Name[1]);
}
public static void addDonor(String[] a1, String[] a2, char[] a3, int[] a4, int [] a5), int index{
Scanner input = new Scanner(System.in);
System.out.print(" Enter the name (first and last):" + " ");
String name = input.nextLine();
System.out.print(" Enter Mobile No.:" + " ");
String phone = input.next();
if (phone.length() < 10 && phone.startsWith("0") == false){
while (true){
System.out.println("Wrong Mobile NO... try again!");
System.out.print(" Enter Mobile No.:" + " ");
phone = input.next();
if (phone.length() > 10 || phone.startsWith("0") == true)
break;
}
}
System.out.print(" Enter Blood Group Type (A, B or O):" + " ");
char blood = input.next().charAt(0);
while (blood != 'a' || blood != 'b' || blood != 'c'){
System.out.println("Wrong Blood Group Type... try again!");
System.out.println(" Enter Blood Group Type (A, B or O):" + " ");
blood = input.next().charAt(0);
if (blood == 'A' || blood == 'B' || blood == 'O')
break;
}
a1[index] = name;
a2[index] = phone;
a3[index] = blood;
a4[index] = 1;
a5[index] = 1;
推荐阅读
- php - 在 elasticsearch 中为查询定义自定义分析器时出错
- python - 如何使用 urllib3 发出 put 请求
- c# - 如何从包含该值的列表中删除一个值?
- java - 使用按钮将文件添加到 JList
- bash - 是否可以将 if 语句放入管道中?
- javascript - 更新按钮上的超链接(ajax json)
- java - 在我的 documentrefernce firesotre 集合中显示的片段 illigleState 异常中
- python - 如何在odoo中使用自定义名称更改上传的附件文件名
- java - 无法创建实例 - 构造函数具有私有访问权限
- python - 查询对象本地化时谷歌云服务器强制关闭