簡體   English   中英

JOptionPane.showmessagedialog

[英]JOptionPane.showmessagedialog

因此,我必須編寫一個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()的幫助下將其打印在輸出面板中。 有人可以幫我嗎? 抱歉,我的英語不好。 我的截止日期是星期一。

嘗試這樣的事情:

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。

在開頭創建一個字符串,例如。

String kLetter = "" ;

每次之后:

System.out.print("*"); 添加kLetter += "*";

System.out.print(" "); 添加kLetter += " ";

System.out.println("*"); 添加kLetter += "*\\n";

然后使用

JOptionPane.showInputDialog(result, null);

在對話框中顯示結果,並記得在對話框中顯示結果后將結果設置為空字符串。

result = "";

暫無
暫無

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

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