-1
import java.io.*;
import java.util.*;

public class DonaldsonDuaneMidtermActivity3A {
    public static void main (String[] args) {
        Scanner keyboard = new Scanner(System.in);
        float annualRate = 0.0F;
        float quarterlyRate = 0.0F;
        double principal = 0.0;
        double interest = 0.0;
        double finalAmount = 0.0;
        byte quarter = 0;
        int year = 0;
        annualRate = 0.05F;                                                             
        System.out.print("Enter the year: ");
        year = Integer.parseInt(keyboard.nextLine());                                   
        System.out.print("Enter the initial principal: ");
        principal = Double.parseDouble(keyboard.nextLine());                            
        System.out.printf("%s%.2f%n", "Principal = ", principal);                       
        System.out.printf("%s%.2f%c%n", "Interest Rate = ", annualRate * 100, '%');
        System.out.printf("%6s%8s%16s%30s%n", "Year", "Quarter", "Interest Earned", >"Amount at end of quarter");
        quarterlyRate = annualRate / 4;                                                 
        quarter = 1;
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);
        principal = finalAmount;
        quarter = 2;                                                                    
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);

        principal = finalAmount;
        quarter = 3;                                                                    
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;                                             
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);

        principal = finalAmount;                                                        
        quarter = 4;                                                                    
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;                                             
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);

        System.exit(0);`enter code here`                                                                
    }
}

第一篇文章,请善待,我已经搜索过类似的程序,但没有看到类似的程序。我会假设我需要在 for 循环中放入更多内容,而不仅仅是我的 4 个不同的部分来遍历它们。我还需要在屏幕上输出一些与没有 for 循环的程序完全相同的内容。我已经玩了一些,但无法根据需要将所有内容打印出来。感谢您帮助一位老人尝试新事物。我也在使用 JCreator,所以我可以在软件下部的输出窗口中获取所有内容。

quarterlyRate = annualRate / 4;

    for (quarter = 1; quarter <= 4; quarter = quarter + 1) {

    interest = principal * quarterlyRate;

    finalAmount = principal + interest;                                             // comment out if uncomment next two lines
    System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);// comment out if uncomment next two lines
    //principal += interest;                                                          // add principal to interest and assign back to principal 

    //System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, principal);  // change finalAmount to principal but it keeps the same output

    principal = finalAmount;
    }                                                                               // end of for loop

    System.exit(0);
4

1 回答 1

1

如果我遵循您的问题,那么您应该使用for循环将兴趣应用于principal然后打印您的消息。那应该看起来像

for (int quarter = 1; quarter <= 4; quarter++) {
  interest = principal * quarterlyRate;                                           
  principal += interest;
  System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, principal);
}
于 2014-10-10T15:05:57.670 回答