大家好,又见面了,我是你们的朋友全栈君。
importjava.util.*;publicclassRetirement2{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);System.out.print(“Howmuchmoneywillyoucontributeeveryyear?”);do…
import java.util.*;
public class Retirement2
{
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.equals(“N”));
}
}
后面input = in.nextLine();这里用next就完全正常,可以输入字符。但是用nextLine程序就自动结束了,求解。
按照你的第一种方法确实解决问题了,可是我想知道in.nextLine()返回的是哪句话,而因此结束了呢?
展开
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/133221.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...