2011-06-10 5 views
1

JOptionPane.ShowMessageDialog에 system.out.print 메시지를 인쇄하려면 어떻게해야합니까? for 루프를 실행하고 for 루프 외부를 실행해야합니다. JOptionPane.ShowMessageDialog 박스의 나는 정말로 시간을 벌어서 정신을 잃었다. 어떤 도움을 주시면 감사하겠습니다. 고맙습니다!JOptionPane.ShowMessageDialog를 인쇄 할 Java system.out.print

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

+0

자바 개념이 오래되었다. 업데이트해야합니다 :) 감사합니다 – LoSciamano

1

시도는 모두 StringBuilder에

의 StringBuilder SB = 새의 StringBuilder()에 대한 를 출력 물품 (0 = 1을 나타내는 int i가 < 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

0

프린터 설정 자신 만의 'System.out'에 사용됩니다. 문자열 작성자로 리디렉션하거나 자신의 프린터 구현을 작성하고 대신 'StringBuilder'에서 모든 인쇄물을 수집하는 데 사용하십시오. 루프 외부에서 수집 된 '문자열'을 가져 와서 대화 상자에 표시하십시오.

'System.out.println'altlogether를 사용하는 것이 좋습니다.하지만 다른 선택의 여지가 없다고 가정합니다.