日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于java中nextline读取空白行的问题

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于java中nextline读取空白行的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在做java作業, 發現了一個問題, 就是nextline其實會接收緩沖區的\r, 使得在程序運行時nextline像是跳過了一樣, 其實不然, 它只是讀取了上一個enter時的\r, 如我的如下功能代碼

public void run() {Scanner scan = new Scanner(System.in);int ord, book_order;int flag = 0;String isbn = new String();String nme = new String();while (flag == 0) {System.out.println("Function List:");System.out.println("1.Show the book list");System.out.println("2.Find book");System.out.println("3.Add book");System.out.println("4.Delete book");System.out.println("0.Exit");System.out.println("Please input the functon order:");ord = scan.nextInt();switch (ord) {case 0: {flag = 1;break;}case 1: {Print();break;}case 2: {Scanner in = new Scanner(System.in);System.out.println("Please input the ISBN of book you want to find:");isbn = in.nextLine();book_order = Find(isbn);if (book_order != 0) {System.out.println("Success!");System.out.println("Name:" + books[book_order].name);System.out.println("ISBN:" + books[book_order].ISBN);System.out.println();} else {System.out.println("Error!No such book!");System.out.println();}break;}case 3: {Scanner in = new Scanner(System.in);System.out.println("Please input the name of the book:");nme = scan.nextLine();System.out.println("Please input the ISBN of the book:");isbn = scan.nextLine();if (Add(nme, isbn)) {System.out.println("Add successfully!");System.out.println();} else {System.out.println("Failed to add!It's out of range!");System.out.println();}break;}case 4: {System.out.println("Please input the ISBN of book you want to delete:");isbn = scan.nextLine();if (Del(isbn)) {System.out.println("Delete successfully!");System.out.println();} else {System.out.println("Failed to delete!No such book!");System.out.println();}break;}}}scan.close();} }

在這里, 我一開始就已經實例化了一個scanner對象, 命名為scan, 然后在輸入功能選項時我們調用了nextInt, 那么在輸入完之后, 整數被讀取進去了, 但是那個\r還留在scan的buffer區域, 所以, 如果我們添加書籍的話, 再次使用scan的nextline來讀取書名的話, nextline就會得到scan的buffer區域中的\r,導致程序錯誤, 所以,在這里看到老師的代碼之后, 得到啟發, 重新新建一個scanner對象, 在這里可以使用Scanner in, 這樣的話, in的buffer區域中就沒有之前存留的\n, 那么這個時候我們再輸入一行, 則不會出現\r被讀取的問題.

public void run() {Scanner scan = new Scanner(System.in);int ord, book_order;int flag = 0;String isbn = new String();String nme = new String();while (flag == 0) {System.out.println("Function List:");System.out.println("1.Show the book list");System.out.println("2.Find book");System.out.println("3.Add book");System.out.println("4.Delete book");System.out.println("0.Exit");System.out.println("Please input the functon order:");ord = scan.nextInt();switch (ord) {case 0: {flag = 1;break;}case 1: {Print();break;}case 2: {Scanner in = new Scanner(System.in);System.out.println("Please input the ISBN of book you want to find:");isbn = in.nextLine();book_order = Find(isbn);if (book_order != 0) {System.out.println("Success!");System.out.println("Name:" + books[book_order].name);System.out.println("ISBN:" + books[book_order].ISBN);System.out.println();} else {System.out.println("Error!No such book!");System.out.println();}break;}case 3: {Scanner in = new Scanner(System.in);System.out.println("Please input the name of the book:");nme = in.nextLine();System.out.println("Please input the ISBN of the book:");isbn = in.nextLine();if (Add(nme, isbn)) {System.out.println("Add successfully!");System.out.println();} else {System.out.println("Failed to add!It's out of range!");System.out.println();}break;}case 4: {System.out.println("Please input the ISBN of book you want to delete:");isbn = scan.nextLine();if (Del(isbn)) {System.out.println("Delete successfully!");System.out.println();} else {System.out.println("Failed to delete!No such book!");System.out.println();}break;}}}scan.close();} }

運行結果如下:

此外, 也可以和cpp一樣, 使用一次scan.nextLine將那個\r讀取.

總結

以上是生活随笔為你收集整理的关于java中nextline读取空白行的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。