我正在为我的一个大学课程学习Java,我需要知道如何在for循环的每12次迭代后暂停我的程序的输出。我有一个循环迭代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));
}
}
}
答案 0 :(得分: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();
一旦停止,您需要按任意字符和返回键或只返回键。
答案 1 :(得分: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();
答案 2 :(得分: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;
}
}
您可以轻松地从互联网上获取任何密钥的密钥代码。
答案 3 :(得分:-1)
你只需要使用System.in.read();阅读用户输入。干杯!