2014-10-10 32 views
-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,因此我可以在软件较低区域的输出窗口中获得所有内容。JAVA,季度兴趣与循环

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

//输入年月日:2005年 //输入初始本金:10000.00 //校长:= 10000.00 //年利率:5.00% //去年同期\t利息在季末\t挣金额 // 2005 1 \t \t \t \t 125.00 10125.00 \t // \t \t 126.56 10251.56 \t // \t \t 128.14 \t \t 10379.70 // 2005年4 \t \t \t 129.75 \t \t 10509.45 通过// // DELIMITeach行,你应该了解信息的栏目,让我知道如果你//不请。 当我复制粘贴我的代码时,格式化在几行上有点偏离,谢谢你帮助一只老狗学习新的技巧。 Humpty Dumpty over 50. – 2014-10-10 14:36:04

+0

你有代码,你有输出,但你在这里完全没有问题。你在问什么? – Makoto 2014-10-10 14:37:02

+0

for循环的目的是什么?请描述我们使用其中的最终目标。例如,它是否应该生成几年的季度报告?要求额外的输入?这将帮助我们确定你实际上在寻找什么。 – Compass 2014-10-10 14:38:36

回答

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

谢谢你,我会试一试,让你知道,我的确如你所做的那样做了陈述,但我并没有想到将5行中的3行放在for循环中。我只用了四分之一行或全部5行重复的原始代码,但没有得到我需要的输出。需要去工作,今晚会再试一次。 – 2014-10-10 22:34:26

+0

我得到它与我的原始代码工作,但通过使用Elliott的代码与校长+ =的兴趣是优雅的。我不得不从printf下面的下一行获取另一行代码。我将尝试发布更改,上次我永远需要发布代码并使其接受。 – 2014-10-11 11:55:26

+0

我已将它运行,并希望发布代码,遇到同样的问题,我第一次,在这个网站上缺乏经验,现在就开始工作。 – 2014-10-11 12:04:17