おそらくこれをカバーする質問がありますが、検索しても見つかりませんでした。アイデアは、次のような文字と行数のユーザー入力を指定してパターンを表示することです。
x
xx
xxx
xxxx
xxxx
xxx
xx
x
しかし、これを行うには JOptionPane を使用する必要があります。ここに私が持っているものがあります:
import javax.swing.JOptionPane;
public class loopPattern {
public static void main(String[] args) {
String in, num, output1 = null, output2 = "";
int lines = 0;
int i, n = 1;
in = JOptionPane.showInputDialog("Please enter the character for the pattern:");
num = JOptionPane.showInputDialog("Please enter the number of lines in the pattern:");
lines = Integer.parseInt(num);
for (i=1; i<=lines; i++) {
for (n=1; n<=lines; n++) {
output1 = (n +"\n");
}
}
for (i=lines; i>=1; i--){
for (n=lines; n>=1; n--){
output2 = (n +"\n");
}
}
JOptionPane.showMessageDialog(null, output1 +output2);
}
}
次に、ユーザーが「OK」を押すたびにこのパターンを繰り返し、「キャンセル」を押すと終了するようにする必要があります。文字列変数に蓄積することを理解できれば、それができると思います。助けてくれてありがとう。