所以我必须写一个DrawKwin.java,它会用小星星(*)打印字母K. 用户给出一个整数参数,如果参数小于4或大于30,程序将终止。 使用该参数,程序将创建与尝试打印字母K的参数一样多的行。 例如,如果用户输入数字6,程序将打印6行试图创建字母K.输入将来自输入面板,字母K将使用joptionpane.showmessagedialog()打印在输出面板中。
以下是没有输出面板代码的代码:
package Askisi_A1;
import javax.swing.JOptionPane;
class DrawKwin {
public static void main(String[] L) {
int line=Integer.parseInt(L[0]); // make L an integer.
if(line <= 4)
{
System.out.println("Program end, wrong argument!");
System.exit(0);
}
else if(line >= 30)
{
System.out.println("Program end, wrong argument!");
System.exit(0);
}
do
{
int mid=line/2; // find the middle.
int gap=0; // 'gap' is for the gap between the stars .
for(int i=0;i<line;i++) //loop for the creation of letter K.
{
if(i==0) gap=mid;
if(i<mid) // if it is before the middle of letter K, start printing stars and gaps but start with gap=middle and the decrease the number of gaps as you change lines.
{
System.out.print("*");
for(int j=gap;j>0;j--) // placement of gaps between the stars.
{
System.out.print(" ");
}
System.out.println("*");
gap--;
}
else if(i==mid && i!=0) // if it is in the middle of letter K, it will print only one star.
{
System.out.println("*");
gap=1;
}
else // if it is past the middle section of letter K, it will continue printing gaps but now the gaps start from 0 and keep increasing at each line.
{
System.out.print("*");
for(int j=0;j<gap;j++) // placement of gaps between the stars.
{
System.out.print(" ");
}
System.out.println("*");
gap++;
}
}
line = Integer.parseInt(JOptionPane.showInputDialog( "Give me a number ",4)); // input from input panel.
}while(line>=4 && line<=30);
}
}
因此,如果用户将数字5作为输入,则输出应如下所示:
* *
* *
*
* *
* *
但我需要在joptionpane.showmessagedialog()的帮助下将其打印在输出面板中。 有人能帮帮我吗? 对不起,如果我的英语不好。 我的截止日期是星期一。
答案 0 :(得分:0)
尝试这样的事情:
do {
int mid = line / 2; // find the middle.
int gap = 0; // 'gap' is for the gap between the stars .
for (int i = 0; i < line; i++) // loop for the creation of letter K.
{
if (i == 0)
gap = mid;
if (i < mid) // if it is before the middle of letter K, start
// printing stars and gaps but start with
// gap=middle and the decrease the number of
// gaps as you change lines.
{
output += "*";
for (int j = gap; j > 0; j--) // placement of gaps between
// the stars.
{
output += " ";
}
output += "*\n";
gap--;
} else if (i == mid && i != 0) // if it is in the middle of
// letter K, it will print only
// one star.
{
output += "*\n";
gap = 1;
} else // if it is past the middle section of letter K, it will
// continue printing gaps but now the gaps start from 0
// and keep increasing at each line.
{
output += "*";
for (int j = 0; j < gap; j++) // placement of gaps between
// the stars.
{
output += " ";
}
output += "*\n";
gap++;
}
}
JOptionPane.showMessageDialog(null, output);
line = Integer.parseInt(JOptionPane.showInputDialog(
"Give me a number ", 4)); // input from input panel.
output = "";
} while (line >= 4 && line <= 30);
构建输出字符串,然后使用输出字符串构建showMessageDialog。
答案 1 :(得分:0)
在开头创建一个String,例如。
String kLetter = ""
;
每次:
System.out.print("*");
加
kLetter += "*";
System.out.print(" ");
加
kLetter += " ";
System.out.println("*");
加
kLetter += "*\n";
然后使用
JOptionPane.showInputDialog(result, null);
在对话框中显示结果,并记住在对话框中显示结果后将结果设置为空字符串。
result = "";