nextline函数_java中next与nextLine的用法

nextline函数_java中next与nextLine的用法importjava.util.*;publicclassRetirement2{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);System.out.print(“Howmuchmoneywillyoucontributeeveryyear?”);do…importjava.util.*;publicc…

大家好,又见面了,我是你们的朋友全栈君。

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()返回的是哪句话,而因此结束了呢?

展开

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133221.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号