0

「名前の数」を表示したい。2 つの名前を入力すると、""あなたは 2 つの名前を入力しました。

import javax.swing.JOptionPane;

public class MyName {
public static void main (String args [ ])

{
    int option;
    String userName;

    do
    {
        option = JOptionPane.showConfirmDialog(null,"Want to enter another Name?");

    } while (option == JOptionPane.YES_OPTION);

    JOptionPane.showMessageDialog(null, "you have entered" + (number of names) + "names");

}
}
4

2 に答える 2

3

反復をカウントするには、次のようにします。

int i=0; // create a counter
do {
    option = JOptionPane.showConfirmDialog(null,"Want to enter another useraName?");
    i++; // increment the counter
} while (option == JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog(null, "you have entered " + i + " names"); // use it

ただし、通常は名前を保存する必要があるため、この問題は発生しません。

List<String> names = new ArrayList<>();
do {
     ...
     names.add(enteredName);
while ...

ループ後、名前の数は ですnames.size()

于 2013-03-17T09:31:42.003 に答える
1

変数を使用して?

import javax.swing.JOptionPane;

public class MyName {
public static void main (String args [ ])

{
    int option;
    String userName;

    int number = 0;
    do
    {
        number++;
        option = JOptionPane.showConfirmDialog(null,"Want to enter another useraName?");

    } while (option == JOptionPane.YES_OPTION);

    JOptionPane.showMessageDialog(null, "you have entered" + (number of names) + "number");

}
}
于 2013-03-17T09:32:12.450 に答える