2017-10-11 18 views
4

こんにちは私はこのフォーラムに新しいとjavaに新しい、私はいくつかのJavaの宿題の紹介に役立つ必要があります。私はロジックの大部分をやった。問題は、100から1000までのすべての数字を1行に10個ずつ表示するプログラムを書くことです。数字は5と6で割り切れます。数字はちょうど1つのスペースで区切られます。私の教授はJoptionpaneウィンドウでそれをやりたいと思っています。私がそれをしようとすると、1つの答えがウィンドウにポップアップします。私の答えは1つのウィンドウ内に正確に1つのスペースで区切られた10行で表示されますか?私の教授は、答えの線を表示するためにエスケープ機能を使用したいと考えています。Joptionpaneに複数の行を表示していますか?

public class FindFactors { 
    public static void main(String[] args) { 
     String message = ""; 
     final int NumbersPerLine = 10; // Display 10 numbers per line 
     int count = 0; // Count the number of numbers divisible by 5 and 6 

     // Test all numbers from 100 to 1,000 

     for (int i = 100; i <= 1000; i++) { 
      // Test if number is divisible by 5 and 6 
      message = message + " " + i; 
      count++; 
      if (count == 10) { 
       message = message + "\r\n"; 
       count = 0; 
      } 
      if (i % 5 == 0 && i % 6 == 0) { 
       count++; // increment count 
       // Test if numbers per line is 10 
       if (count % NumbersPerLine == 0) 
        JOptionPane.showMessageDialog(null, i); 
       else 
        JOptionPane.showMessageDialog(null, (i + " ")); 
      } 
     } 
    } 
} 

答えて

0

JOptionPane、画像を表示する機能、テキストやその他のコンポーネントを持っているこの特定のケースのために、あなたはJLabelに数字のすべての行を追加し、それにそのJLabelを追加し、独自のJPanelを作成することもできます。

import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class MultiLineOptionPane { 
    private JPanel pane; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new MultiLineOptionPane()::createAndShowGui); 
    } 

    public void createAndShowGui() { 
     pane = new JPanel(); 
     pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); 

     StringBuilder sb = new StringBuilder(); //This object will be used to concatenate in the next for loop 
     for (int i = 0; i < 500; i++) { //Sample for loop 
      if ((i) % 10 == 0) { //Every 10 numbers we restart the StringBuilder 
       pane.add(new JLabel(sb.toString())); //We add a new JLabel to the JPanel with the contents of the StringBuilder 
       sb.delete(0, sb.length()); //We restart the StringBuilder 
      } else { 
       sb.append(i); //We append the current number to the StringBuilder 
       sb.append(" "); //We append a space after the number 
      } 
     } 
     pane.add(new JLabel(sb.toString())); //We add the last line of numbers in the StringBuilder to the pane 

     JOptionPane.showMessageDialog(new JFrame(), pane, "Numbers", JOptionPane.PLAIN_MESSAGE); //We display the message 
    } 
} 

上記のプログラムの出力は(トリミングまたはそれは背が高すぎるだろう)のようになります。

enter image description here

1

、以下のアプローチを参照してください、あなたのコードに若干の変更となりますしてください必要な出力。

public class FindFactors { 
public static void main(String[] args) { 
final int NumbersPerLine = 10; // Display 10 numbers per line 
int count = 0; // Count the number of numbers divisible by 5 and 6 
// Test all numbers from 100 to 1,000 
String numbersPerLine = ""; 
for (int i = 100; i <= 1000; i++) { 
    // Test if number is divisible by 5 and 6 
    if (count == 10) {   
     count = 0; 
    } 
    if (i % 5 == 0 && i % 6 == 0) { 
     numbersPerLine =numbersPerLine+" "+i; 
     count++; // increment count 
     // Test if numbers per line is 10 

     if (count % NumbersPerLine == 0) 
      numbersPerLine =numbersPerLine+"\n"; 
    } 
} 
JOptionPane.showMessageDialog(null, numbersPerLine); 
} 
} 
+0

**注**私の解決策では、私は賢明な、効率的なパフォーマンスではありませんが、それは私がそのようにやって実行し、単一のプログラムのように、ループ内で文字列の連結を行っています。プロダクションレベルのコードでは、この文字列連結アプローチに従わないでください。 –

+0

まずはあなたのソリューションに感謝します!しかし、私があなたのソリューションを実行すると、最初の行には9つの回答があり、1つの行に10の回答が必要です。 –

+0

** forループ**の先頭にある最初の 'count ++'インクリメントを削除するだけで、未使用のString変数**メッセージ**を削除してコードを処理してクリーンアップする必要があります。 –

関連する問題