2011-06-10 50 views
1

如何将system.out.print消息打印到JOptionPane.ShowMessageDialog中我需要它运行for循环,然后在for循环之外运行我需要它在里面显示一个JOptionPane.ShowMessageDialog框。我真的很沮丧的时间。我将不胜感激任何帮助。谢谢!Java system.out.print打印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); 


} 

任何理由为什么这不工作?

回答

2

使用StringBuffer。

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

对于这种情况,StringBuilder在JDK 5及更高版本中被优先考虑。 – RonK 2011-06-10 21:48:30

+0

我的java观念正在变老。我需要更新:)谢谢 – LoSciamano 2011-06-10 21:50:32

1

尝试写入输出到StringBuilder的

StringBuilder的SB =新的StringBuilder() 的for(int i = 0;我< numberOfLoans;我++){

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(); 
+0

非常感谢!这似乎工作,但最终不会打印任何内容。我没有收到任何错误消息。我的代码在顶部。谢谢! – PittsburghCoder 2011-06-10 22:13:18

0

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

我会建议不要使用'System.out.println',但我想你没有别的选择。