說(shuō)明:我在下面的代碼中使用了幾個(gè)掃描儀語(yǔ)句。我首先閱讀“地址”,然后閱讀“手機(jī)號(hào)碼”。然后是來(lái)自用戶的一些隨機(jī)內(nèi)容。當(dāng)我使用 adress=sc.next()它從用戶(無(wú)空格)讀取地址的字符串值并轉(zhuǎn)到下一個(gè)掃描語(yǔ)句,即mobile=sc.nextBigInteger()。通過(guò)使用此方法,我無(wú)法讀取“地址(字符串)”中的空格,并且將運(yùn)行時(shí)錯(cuò)誤作為 inputMismatchException 拋出?,F(xiàn)在,如果我使用adress=sc.NextLine,程序會(huì)直接跳轉(zhuǎn)到mobile=sc.nextBigInteger().在上述情況和以下代碼中,如何將空格讀取為輸入。我如何保護(hù)自己免受運(yùn)行時(shí)錯(cuò)誤的影響。我在論壇上遇到過(guò)類似的問(wèn)題,但沒(méi)有一個(gè)是令人滿意的。謝謝你。(問(wèn)我是否需要有關(guān)問(wèn)題的更多信息)預(yù)期: 輸入(在地址字符串中):印度馬哈拉施特拉邦浦那輸出(在顯示功能中):印度浦那馬赫拉施特拉邦。究竟發(fā)生了什么: 如果輸入(在地址字符串中):浦那輸出(在顯示功能中):浦那如果輸入(在地址字符串中):印度浦那(現(xiàn)在只要我輸入字符串并在那里點(diǎn)擊輸入,我就會(huì)收到運(yùn)行時(shí)錯(cuò)誤作為輸入不匹配異常)> JAVA public class Data { Scanner sc = new Scanner(System.in); String adress; BigInteger AcNo; BigInteger mobile; String ifsc; void getData() { System.out.println("Welcome to Bank System"); System.out.println("Please Enter the adress :"); adress= sc.next(); System.out.println("Enter the Mobile Number"); mobile = sc.nextBigInteger(); System.out.println("Enter the Account Number"); AcNo = sc.nextBigInteger(); System.out.println("Enter the IFSC Code"); ifsc= sc.next(); } public static void main(String[] args) { Data d=new Data(); d.getData(); } }
添加回答
舉報(bào)
0/150
提交
取消