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);
答案 0 :(得分: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);
}