亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用計數器向數組添加值

使用計數器向數組添加值

一只甜甜圈 2022-09-28 10:55:52
在過去的1.5個月里,我一直在學習java?,F在,講師要求我們創建一個程序,該程序從用戶那里獲取名稱和電話號碼(但以一種方法),直到用戶輸入“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++;}
查看完整描述

3 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

問題是你在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;


查看完整回答
反對 回復 2022-09-28
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

這里有很多錯誤。


首先,您應該在課堂上真正收集所需的所有信息。這樣,您可以輕松地將其全部存儲在一個數組中。


public class donor{

String name;

String mobile

...

}

其次,因為你不知道你得到了多少輸入,數組真的是一種愚蠢的存儲方式。如果您使用列表,您可以簡單地執行以下操作:


List<Donor> donors = new ArrayList<>();

donors.add(donor);

如果需要使用數組,可以嘗試使用計數器。我可能會這樣做:


public static void main(String[] args) 

{

    Scanner input = new Scanner(System.in);


    String[] name = new String[20];

    String[] mobile = new String[20];

    char[] bloodGroup = new char[20];

    int[] countODonation = new int[20];

    int[] bloodStock = new int[20];

    int counter = 0;

    boolean continue = true;

    while (continue){

        displayMainMenu();


    String choice = readAndVerify();

        switch (choice){

            case "1":

                name[counter] = readName();

                ...

                counter++;            

                break;

        }

       if (choice.equals("e"))

          continue = false; 

      }

}

但是,再一次,您應該真正使用一個類來表示捐贈者的東西。還有一個列表。


查看完整回答
反對 回復 2022-09-28
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

這并不是一個真正的java問題,而是一個調試問題。您的計數器是其所在方法的本地計數器,因此每次調用該方法時,計數器都會重置。

變量具有作用域。變量的作用域取決于定義它的位置。在方法塊內部(或參數部分內部),變量將與方法塊一樣長。如果在類中定義為非靜態,則它將與實例一樣長。如果在類中定義為靜態,它將與類一樣長(大多數情況下這意味著永遠)

在你的情況下,你有2個選項:要么使變量靜態,要么在addDonor方法之外定義它,然后按值將其傳遞到addDonor方法中(因此你不能在addDonor內部增加它,無論你在哪里調用addDonor,都可以這樣做)。

如果使用靜態變量,則代碼保持不變。靜態變量通常在類的頂部定義。

如果將傳遞變量向下移動到 addDonor 中,則 main 方法將負責保留和遞增計數器變量。確保它僅在成功的迭代中遞增。

您還可以執行其他操作,但它們需要您了解對象。


查看完整回答
反對 回復 2022-09-28
  • 3 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號