JAVA中nextLine()求解惑

xinxinran12 2013-09-26 10:35:23
如下程序:
import java.util.*;

public class TestInput1
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("How much money will you contribute every year? ");
double payment = in.nextDouble();


System.out.print("Interest rate in %: ");
double interestRate = in.nextDouble();


double balance = 0;
int year = 0;

String input;

// update account balance while user isn't ready to retire
do
{
// add this year's payment and interest
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;

year++;

// print current balance
System.out.printf("After year %d, your balance is %,.2f%n", year, balance);

// ask if ready to retire and get input
System.out.print("Ready to retire? (Y/N) ");
input = in.nextLine();
}
while (input.equalsIgnoreCase("N") || (input.length()==0));
}
}
运行后,为什么在第一个Ready to retire? (Y/N) 之后紧接着的是After year 2, your balance is 146,363.00,而不是空格呢?
How much money will you contribute every year? 70000
Interest rate in %: 3
After year 1, your balance is 72,100.00
Ready to retire? (Y/N) After year 2, your balance is 146,363.00
Ready to retire? (Y/N)
...全文
51 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧