使用计数器向数组添加值

在过去的1.5个月里,我一直在学习java。现在,讲师要求我们创建一个程序,该程序从用户那里获取名称和电话号码(但以一种方法),直到用户输入“E”。然后,程序应打印存储在所有数组中的所有信息。


该程序有一个主菜单,用户将输入“1”以创建一个帐户(姓名和电话号码),然后主菜单再次出现,用户创建另一个帐户,依此类推...直到他从菜单中选择另一个选项或输入“E”以存在并打印所有帐户的摘要。


我的问题是,我试图创建一个计数器作为数组中每个帐户点的引用(数组中的索引);因此,每次用户输入名称和数字后,计数器加1,数组索引加1并移动到下一个位置...但这不起作用。


我没有完成代码,在选择1处停下来测试创建帐户方法


    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++;


}


一只甜甜圈
浏览 104回答 3
3回答

MM们

问题是你在addDonor方法中创建变量索引。因此,每次调用该方法时,都会创建一个值为 0 的新变量,这就是它不会移动的原因。您应该在方法外部创建可变参数,并将其作为参数传递。像这样:public static void main(String[] args) {Scanner input = new Scanner(System.in);&nbsp; &nbsp; String[] Name = new String[20];&nbsp; &nbsp; String[] Mobile = new String[20];&nbsp; &nbsp; char[] Blood_Gp = new char[20];&nbsp; &nbsp; int[] Count_o_Donation = new int[20];&nbsp; &nbsp; int[] Blood_Stock = new int[20];&nbsp; &nbsp; int index = 0;&nbsp; &nbsp; while (true){&nbsp; &nbsp; &nbsp; &nbsp; displayMainMenu();&nbsp; &nbsp; readAndVerify();&nbsp; &nbsp; String choice = readAndVerify();&nbsp; &nbsp; &nbsp; &nbsp; switch (choice){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addDonor(Name, Mobile, Blood_Gp, Count_o_Donation, Blood_Stock, index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;if (choice.equals("e"))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(Name[0]);&nbsp; &nbsp; System.out.println(Name[1]);}&nbsp; &nbsp;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("&nbsp; Enter the name (first and last):" + " ");String name = input.nextLine();System.out.print("&nbsp; Enter Mobile No.:" + " ");String phone = input.next();if (phone.length() < 10 && phone.startsWith("0") == false){&nbsp; &nbsp; while (true){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Wrong Mobile NO... try again!");&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; Enter Mobile No.:" + " ");&nbsp; &nbsp; &nbsp; &nbsp; phone = input.next();&nbsp; &nbsp; &nbsp; &nbsp; if (phone.length() > 10 || phone.startsWith("0") == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}System.out.print("&nbsp; Enter Blood Group Type (A, B or O):" + " ");char blood =&nbsp; input.next().charAt(0);&nbsp; &nbsp; while (blood != 'a' || blood != 'b' || blood != 'c'){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Wrong Blood Group Type... try again!");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; Enter Blood Group Type (A, B or O):" + " ");&nbsp; &nbsp; &nbsp; &nbsp; blood =&nbsp; input.next().charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; if (blood == 'A' || blood == 'B' || blood == 'O')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; a1[index] = name;&nbsp; &nbsp; a2[index] = phone;&nbsp; &nbsp; a3[index] = blood;&nbsp; &nbsp; a4[index] = 1;&nbsp; &nbsp; a5[index] = 1;

弑天下

这里有很多错误。首先,您应该在课堂上真正收集所需的所有信息。这样,您可以轻松地将其全部存储在一个数组中。public class donor{String name;String mobile...}其次,因为你不知道你得到了多少输入,数组真的是一种愚蠢的存储方式。如果您使用列表,您可以简单地执行以下操作:List<Donor> donors = new ArrayList<>();donors.add(donor);如果需要使用数组,可以尝试使用计数器。我可能会这样做:public static void main(String[] args)&nbsp;{&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; String[] name = new String[20];&nbsp; &nbsp; String[] mobile = new String[20];&nbsp; &nbsp; char[] bloodGroup = new char[20];&nbsp; &nbsp; int[] countODonation = new int[20];&nbsp; &nbsp; int[] bloodStock = new int[20];&nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; boolean continue = true;&nbsp; &nbsp; while (continue){&nbsp; &nbsp; &nbsp; &nbsp; displayMainMenu();&nbsp; &nbsp; String choice = readAndVerify();&nbsp; &nbsp; &nbsp; &nbsp; switch (choice){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name[counter] = readName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;if (choice.equals("e"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue = false;&nbsp;&nbsp; &nbsp; &nbsp; }}但是,再一次,您应该真正使用一个类来表示捐赠者的东西。还有一个列表。

慕神8447489

这并不是一个真正的java问题,而是一个调试问题。您的计数器是其所在方法的本地计数器,因此每次调用该方法时,计数器都会重置。变量具有作用域。变量的作用域取决于定义它的位置。在方法块内部(或参数部分内部),变量将与方法块一样长。如果在类中定义为非静态,则它将与实例一样长。如果在类中定义为静态,它将与类一样长(大多数情况下这意味着永远)在你的情况下,你有2个选项:要么使变量静态,要么在addDonor方法之外定义它,然后按值将其传递到addDonor方法中(因此你不能在addDonor内部增加它,无论你在哪里调用addDonor,都可以这样做)。如果使用静态变量,则代码保持不变。静态变量通常在类的顶部定义。如果将传递变量向下移动到 addDonor 中,则 main 方法将负责保留和递增计数器变量。确保它仅在成功的迭代中递增。您还可以执行其他操作,但它们需要您了解对象。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java