当前位置:编程学习 > JAVA >>

如何将nextLine()取到的回车或空格打印到屏幕上?

如下程序:
import java.util.*;

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

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

      System.out.println("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.println("Ready to retire? (Y/N) ");
         input = in.nextLine();
      }
      while (input.equalsIgnoreCase("N") || (input.length()==0));
   }
}
运行后,怎么才能将第一次运行input = in.nextLine();的结果(回车or空格)打印出来?
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) --------------------编程问答-------------------- 怎么才能将第一次运行input = in.nextLine();的结果(回车or空格)打印出来?
有点看不懂,在说清楚一点呗,input不是要输入Y/N的吗  怎么是回来or空格呢? --------------------编程问答-------------------- 就是在input = in.nextLine();读取之前,总共有两次读取,在第二次double interestRate = in.nextDouble();读取的时候,我输入一个数,然后回车,然后这次只读取输入的这个数,但是还有个回车字符没有读取,所以当运行到input = in.nextLine();的时候,nextLine()应该会直接去读取回车 --------------------编程问答-------------------- 不知道我的理解对还是不对
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,