看板 java 關於我們 聯絡資訊
如題 在練習物件時遇到了這個問題 原本的程式碼是 public void FirstCall(){ System.out.print("輸入字串:"); Str = in.InputStr(); System.out.print("起始位置:"); Pos = in.InputPos(); System.out.print("新字串:"); NewString = in.InputNewString(); } 但是這樣打我只輸入了字串跟起始位置 程式就結束了 但如果改成 public void FirstCall(){ System.out.print("輸入字串:"); Str = in.InputStr(); System.out.print("新字串:"); NewString = in.InputNewString(); System.out.print("起始位置:"); Pos = in.InputPos(); } 或是 public void FirstCall() { Scanner input= new Scanner(System.in); System.out.print("輸入字串:"); Str = in.InputStr(); System.out.print("起始位置:"); Pos = in.InputPos(); System.out.print("新字串:"); NewString = input.nextLine(); } 這樣子就沒有問題了 想請問各位大大這是甚麼原因呢 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.120.249.99 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1477723247.A.8EC.html
pttworld: InputStr()怎麼實作?I大寫。 10/29 16:26
gn301046: https://imgur.com/a/FjCoJ 實做方法 10/29 17:03
gn301046: In in = new In(); 10/29 17:08
pttworld: 老問題又是吃到回車或所謂換行字元。 10/29 22:06
pttworld: 實作可以判斷如果字串長度為零則迴圈等輸入不回傳。 10/29 22:07
gn301046: 請問p大 什麼是吃到回車或換行字元呢@@ 10/30 00:07
pttworld: PC的標準輸入之一是鍵盤,吃了Enter鍵代表的字元。 10/30 10:18
gn301046: 感謝P大解說 10/30 14:53