簡體   English   中英

java:如何在再次循環時不保存代碼中的值?

[英]java: How Not to save values from the code when looping back again?

當程序再次循環時,我如何不存儲程序中的值。 例如,如果我打算輸入兩個家庭,首先我會詢問第一個家庭的詳細信息並顯示他們的名字,然后我想使用相同的變量來收集下一個家庭並顯示他們的名字,而無需存儲來自以前的家庭。

public static void main(String[] args) {
    // TODO code application logic here
    String ans; 
    String res;
    double cont;
    int cot;
    String name;
    String order = "";

    do {

    ans = JOptionPane.showInputDialog(null,"What is the name of the "
            + "family?" );

    res = JOptionPane.showInputDialog(null, "How many member in the " + ans +
            " family?");

    cot = Integer.parseInt(res); // Converts res String to a number 

     for (int count = 1; count < cot; count ++)  {

        name = JOptionPane.showInputDialog(null, " Enter first name: " + count);
        order += name + " " + ans + "\n";
    }

    JOptionPane.showMessageDialog(null, "Members of the " + ans
            + " Family" + "\n" + order);

    cont = JOptionPane.showConfirmDialog(null, "Do you want to add another "
            + "family", "Membership", JOptionPane.YES_NO_OPTION);

    }while (cont == JOptionPane.YES_OPTION);

     if (cont == JOptionPane.NO_OPTION){
        JOptionPane.showMessageDialog(null," Come Back Again");

    }

}

}

您的訂單變量是您要添加名稱的字符串。 只需在循環開始時重置它:

...
do {
order = "";
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM