【发布时间】:2014-10-15 12:30:11
【问题描述】:
所以我必须编写一个 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() 的帮助下将其打印在输出面板中。 有人可以帮我吗? 对不起,如果我的英语不好。 我的截止日期是星期一。
【问题讨论】:
-
构建字符串 \n 是换行符。一旦你构建了一个代表 K 的字符串,然后使用 joptionpane.showmessagedialog() 来显示构建的字符串。
标签: java swing joptionpane