importjava.util.*;publicclassRetirement2{publicstaticvoidmainString[]args){Scannerin=newScannerSystem.in);System.out.print“Howmuchmoneywillyoucontributeeveryyear?”);do…
import java.util.*;
public class Retirement2
{
public static void mainString[] args)
{
Scanner in = new ScannerSystem.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.equals“N”));
}
}
后面input = in.nextLine);这里用next就完全正常,可以输入字符。但是用nextLine程序就自动结束了,求解。
按照你的第一种方法确实解决问题了,可是我想知道in.nextLine)返回的是哪句话,而因此结束了呢?
展开