Java system.out.print 打印 JOptionPane.ShowMessageDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6312321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:20:17  来源:igfitidea点击:

Java system.out.print to print JOptionPane.ShowMessageDialog

javajoptionpane

提问by PittsburghCoder

How can I print system.out.print message into a JOptionPane.ShowMessageDialog I need it to run through the for loop and then outside the for loop I need it to display inside of a JOptionPane.ShowMessageDialog box. I am really crunched for time. I would greatly appreciate any help. Thank You!

如何将 system.out.print 消息打印到 JOptionPane.ShowMessageDialog 我需要它通过 for 循环运行,然后在 for 循环之外我需要它显示在 JOptionPane.ShowMessageDialog 框内。我真的很赶时间。我将不胜感激任何帮助。谢谢!

    public static void main(String[] args) {

    //A. Enter the Number Of Loans to compare
    String numberOfLoansString = JOptionPane.showInputDialog("Enter the Number Of Loans to Compare"); 
    //Convert numberOfLoansString to int
    int numberOfLoans = Integer.parseInt(numberOfLoansString);

    //B. Enter the Selling Price of Home
    String sellingPriceString = JOptionPane.showInputDialog("Enter the Loan Amount");
    //Convert homeCostString to double
    double sellingPrice = Double.parseDouble(sellingPriceString);

    //C. Enter the Down Payment on the Home
    String downPaymentString = JOptionPane.showInputDialog("Enter the down payment on the Home");
    double downPayment = Double.parseDouble(downPaymentString);

    //Get the loanAmount by Subtracting the Down Payment from homeCost
    double loanAmount = sellingPrice - downPayment;

    //D. Ask the following for as many number of loans they wish to compare
    //D1 Get the interest rate
    double[] annualInterestRatesArray = new double[numberOfLoans];
    double[] monthlyInterestRateArray = new double[numberOfLoans];
    int[] numberOfYearsArray = new int[numberOfLoans];
    double[] monthlyPaymentArray = new double[numberOfLoans];
    double[] totalPaymentArray = new double[numberOfLoans];
    int counter = 1;

    for (int i=0; i < numberOfLoans; i++)
    {
        String annualInterestRateString = JOptionPane.showInputDialog("Enter the interest rate for Scenario " + counter);
        double annualInterestRate = Double.parseDouble(annualInterestRateString);
        annualInterestRatesArray[i] = (annualInterestRate);

        //Obtain monthly interest rate
        double monthlyInterestRate = annualInterestRate / 1200;
        monthlyInterestRateArray[i] = (monthlyInterestRate);

        //D2 Get the number of years
        String numberOfYearsString = JOptionPane.showInputDialog("Enter the number of years for Scenario " + counter);
        int numberOfYears = Integer.parseInt(numberOfYearsString);
        numberOfYearsArray[i] = (numberOfYears);

        //Calculate monthly payment
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
        //Format to keep monthlyPayment two digits after the decimal point
        monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
        //Store monthlyPayment values in an array
        monthlyPaymentArray[i] = (monthlyPayment);

        //Calculate total Payment
        double totalPayment = monthlyPaymentArray[i] * numberOfYears * 12;
        //Format to keep totalPayment two digits after the decimal point
        totalPayment = (int)(totalPayment * 100) / 100.0;
        totalPaymentArray[i] = (totalPayment);

        counter++;
    }

    StringBuilder sb = new StringBuilder();
            for (int i = 0; i < numberOfLoans; i++) {
                sb.append(String.format("\n", sellingPrice, downPayment, loanAmount, annualInterestRatesArray[i], numberOfYearsArray[i], monthlyPaymentArray[i]));
            }
    String toDisplay=sb.toString();

    JOptionPane.showMessageDialog(null, sb.toString(), toDisplay, JOptionPane.INFORMATION_MESSAGE);


}

Any reason why this isn't Working?

这不起作用的任何原因?

回答by LoSciamano

Use a StringBuffer.

使用字符串缓冲区。

 StringBuffer sb=new StringBuffer();
 for (int i = 0; i < numberOfLoans; i++) {
      sb.append(your_string);
 }
 JOptionPane.showMessageDialog(parent,sb.toString());

回答by KrzyH

Try to write output to StringBuilder

尝试将输出写入 StringBuilder

StringBuilder sb = new StringBuilder() for (int i = 0; i < numberOfLoans; i++) {

StringBuilder sb = new StringBuilder() for (int i = 0; i < numberOfLoans; i++) {

    sb.append(String.format("%d\t%s\t%d   %.2f   %.2f\n", sellingPrice, downPayment, loanAmount, annualInterestRatesArray[i], numberOfYearsArray[i], monthlyPaymentArray[i]));

}
String toDisplay=sb.toString();

回答by RonK

Set the printer used by 'System.out' with your own. Either redirect to a string writer or create your own Printer implementation and use it instead to collect all the printed output in a 'StringBuilder'. Once outside of the loop take the collected 'String' and display it on the dialog.

使用您自己的设置“System.out”使用的打印机。重定向到字符串编写器或创建您自己的打印机实现并使用它来收集“StringBuilder”中的所有打印输出。一旦在循环之外,将收集到的“字符串”显示在对话框中。

I would recommend against using 'System.out.println' altlogether, but I assume you have no other choice.

我建议不要使用“System.out.println”altlogether,但我认为您别无选择。