2

我正在为我的一门大学课程学习 Java,我需要知道如何在每 12 次 for 循环迭代后暂停程序的输出。我有一个迭代 360 次的循环,我希望每 12 个项目循环输出输出停止,直到按下一个键。我做了很多搜索,但我发现的所有内容都在更高级的上下文中,我不太明白代码中还发生了什么。这个程序没有 GUI,它非常简单,我可以在这里粘贴所有代码:

public class MyMain

{      
    static double principal = 200000.00;
    static double interest = .0575;
    static int term = 360;
    static DecimalFormat currency = new DecimalFormat("###,###.##");

    public static void main(String[] args)
    {

        MortgageCalculator mortgageWeek2 = new MortgageCalculator(principal, interest, term);
        double monthlyPayment = mortgageWeek2.calculate();
        System.out.println("Welcome to my Mortgage Calculator.");
        System.out.println("The principal on this loan is $" + currency.format(principal) + ".");
        System.out.println("The interest on this loan is " + (interest * 100) + "%.");
        System.out.println("The term on this loan is " + term + " months (" + term / 12 + " years).");
        System.out.println("Payments on this loan will be $" + currency.format(monthlyPayment));

        double balanceRemaining = 200000.0;



        for (int i = 0; i < 30 * 12; i++)
        {
            double interestPaid = balanceRemaining * .0575 / 12;
            double principalPaid = monthlyPayment - interestPaid;
            balanceRemaining = balanceRemaining - principalPaid;
            System.out.println("Month " + (i + 1) + " \tPayment Amount: $" + currency.format(monthlyPayment) + 
                    "\tInterest Paid: $" + currency.format(interestPaid) + 
                    " \tPrincipal Paid: $" + currency.format(principalPaid) + 
                    "    \tBalance Remaining: $" + currency.format(balanceRemaining));
        }
      }
    }
4

4 回答 4

2

for循环分成两段并input在外循环中添加一条语句,如下所示:

    Scanner input = new Scanner(System.in);
    String userInput = null;
    for (int i = 0; i < 30; i++)
    {
      for (int j = 0; j < 12; j++)
      {
        double interestPaid = balanceRemaining * .0575 / 12;
        double principalPaid = monthlyPayment - interestPaid;
        balanceRemaining = balanceRemaining - principalPaid;
        System.out.println("Month " + (i*12+j + 1) + 
                 " \tPayment Amount: $" + currency.format(monthlyPayment) + 
                "\tInterest Paid: $" + currency.format(interestPaid) + 
                " \tPrincipal Paid: $" + currency.format(principalPaid) + 
                "    \tBalance Remaining: $" + currency.format(balanceRemaining));
      }
      userInput = input.nextLine();
    }
    input.close();

一旦它停止,您需要按任意字符和返回键或只按返回键。

于 2012-11-15T04:43:18.390 回答
1

最好的方法是创建一个执行打印的内部循环(有 12 次迭代),并用一个等待输入的循环包装它。所以你有了

Scanner input = new Scanner(System.in);
String in = null;  


for (int j = 0; j < 30; j++) {
      for (int i = 0; i < 12; i++)
      {
        double interestPaid = balanceRemaining * .0575 / 12;
        double principalPaid = monthlyPayment - interestPaid;
        balanceRemaining = balanceRemaining - principalPaid;
        System.out.println("Month " + (i + 1) + " \tPayment Amount: $" + currency.format(monthlyPayment) + 
                "\tInterest Paid: $" + currency.format(interestPaid) + 
                " \tPrincipal Paid: $" + currency.format(principalPaid) + 
                "    \tBalance Remaining: $" + currency.format(balanceRemaining));
    }

    System.out.println("Press any key.");
    in = input.next();

 }
 input.Close();
于 2012-11-15T04:44:13.397 回答
0

您可以使用“KeyPress”事件来检查是否按下某个键的天气。您还可以找出按下了哪个键然后您“中断”以停止循环迭代......希望有帮助

这是一个例子

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    switch( keyCode ) { 
        case KeyEvent.VK_UP:
            // handle up 
            break;
        case KeyEvent.VK_DOWN:
            // handle down 
            break;
        case KeyEvent.VK_LEFT:
            // handle left
            break;
        case KeyEvent.VK_RIGHT :
            // handle right
            break;
     }
} 

您可以轻松地从互联网上获取任何键的键码。

于 2012-11-15T04:45:30.777 回答
-1

你只需要使用 System.in.read(); 读取用户输入。干杯!

于 2012-11-15T04:44:29.570 回答