Java使用Scanner讀取多行輸入跳出問題
阿新 • • 發佈:2019-02-03
原始碼如下:
Scanner in = new Scanner(System.in); System.out.println("Pls insert Title"); ja.setJTitle(in.nextLine()); System.out.println("Pls insert Number"); ja.setJNumber(in.nextInt()); System.out.println("Pls insert Pay"); ja.setJPay(in.nextInt()); System.out.println("Pls insert Description"); ja.setJDes(in.nextLine()); in.close(); Job.displayInfo();
程式在讀取JPay後執行println後自動跳過JDes讀取,直接至dispalyInfo
執行結果如:
經過參考帖子:http://blog.csdn.net/xiao_niu_1/article/details/8240650 後修改程式碼為:
Scanner in = new Scanner(System.in); System.out.println("Pls insert Title"); ja.setJTitle(in.nextLine()); System.out.println("Pls insert Number"); ja.setJNumber(in.nextInt()); System.out.println("Pls insert Pay"); ja.setJPay(in.nextInt()); in.nextLine(); System.out.println("Pls insert Description"); ja.setJDes(in.nextLine()); in.close(); Job.displayInfo();
在讀取JPay後新增in.nextLine()
執行結果: