如何在输出中显示整个计算?

时间:2022-05-05 22:36:58

Good evening,

晚上好,

I'm a beginner in java and I was assigned to code a program to decompose prime number. This is what I've got so far.

我是java的初学者,我被分配编码程序来分解素数。这是我到目前为止所得到的。

package introductionProgramming;

import javax.swing.JOptionPane;

public class Primes {
                public static void main(String[] args) {
                int primo;

    primo = Integer.parseInt(JOptionPane.showInputDialog("Inform prime number: "));

        while (prime % 2 == 0) {
            prime = prime / 2;
        }
        while (prime % 3 == 0) {
            prime = prime / 3;
        }
        while (prime % 5 == 0) {
            prime = prime / 5;
        }

        JOptionPane.showMessageDialog(null, prime);
                }
    }

So the decomposition part seems to work but I need the output, if entered the number 180, to look similar to this:

所以分解部分似乎工作但我需要输出,如果输入数字180,看起来类似于这样:

180  2
90   2
45   3
15   3
5    5
1

I have no clue how to do it.

我不知道怎么做。

1 个解决方案

#1


1  

To send the calcultation as a whole to the output, you will need to gather all results and send it as a whole. To achieve this, when finding each result, you will add the appropriate text to a StringBuffer object, that will gather the results where ultimately it will be displayed. Below is an example of your code.

要将calcultation作为一个整体发送到输出,您需要收集所有结果并将其作为一个整体发送。为实现此目的,在查找每个结果时,您将向StringBuffer对象添加适当的文本,该对象将收集最终将显示的结果。以下是您的代码示例。

public class Primes {
    public static void main(String[] args) {


    int prime = Integer.parseInt(JOptionPane.showInputDialog("Inform prime number: "));
    StringBuffer resultsBuffer = new StringBuffer();

    while (prime % 2 == 0) {
        resultsBuffer.append(prime+" "+2+"\n");
        prime = prime / 2;

    }
    while (prime % 3 == 0) {
        resultsBuffer.append(prime+" "+3+"\n");
        prime = prime / 3;

    }
    while (prime % 5 == 0) {
        resultsBuffer.append(prime+" "+5+"\n");
        prime = prime / 5;

    }

    resultsBuffer.append(prime+" "+1+"\n");



    JOptionPane.showMessageDialog(null, resultsBuffer);
    }
}

#1


1  

To send the calcultation as a whole to the output, you will need to gather all results and send it as a whole. To achieve this, when finding each result, you will add the appropriate text to a StringBuffer object, that will gather the results where ultimately it will be displayed. Below is an example of your code.

要将calcultation作为一个整体发送到输出,您需要收集所有结果并将其作为一个整体发送。为实现此目的,在查找每个结果时,您将向StringBuffer对象添加适当的文本,该对象将收集最终将显示的结果。以下是您的代码示例。

public class Primes {
    public static void main(String[] args) {


    int prime = Integer.parseInt(JOptionPane.showInputDialog("Inform prime number: "));
    StringBuffer resultsBuffer = new StringBuffer();

    while (prime % 2 == 0) {
        resultsBuffer.append(prime+" "+2+"\n");
        prime = prime / 2;

    }
    while (prime % 3 == 0) {
        resultsBuffer.append(prime+" "+3+"\n");
        prime = prime / 3;

    }
    while (prime % 5 == 0) {
        resultsBuffer.append(prime+" "+5+"\n");
        prime = prime / 5;

    }

    resultsBuffer.append(prime+" "+1+"\n");



    JOptionPane.showMessageDialog(null, resultsBuffer);
    }
}