亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何允許我的程序根據用戶輸入的類型執行某些操作?

如何允許我的程序根據用戶輸入的類型執行某些操作?

慕勒3428872 2022-09-07 17:27:53
我已經用Java創建了一個“汽車金融”計算器,但是我想確保不僅涵蓋了快樂的道路。當用戶輸入字符串時,程序將退出,因為它需要一個整數。然后我想,如果我將輸入設置為字符串,然后將其轉換為整數,但是我只希望在輸入被識別為整數的情況下進行此轉換...如果這有意義的話。if(a.equalsIgnoreCase("Blue Car")) {        System.out.println("This car is £9,000");        Scanner input = new Scanner(System.in);        System.out.println("Please type in your deposit amount.");         String value = "";        int intValue;        value = input.nextLine();         try {        intValue = Integer.valueOf(value);        } catch (NumberFormatException e) {        System.out.println("Please print only numbers!");        }                   if(value < 9000) {        System.out.println("The price of the car after your deposit is: " + (9000 - intValue));         System.out.println("Please confirm the left over price after your deposit by typing it in.");        int value1 = 0;        value1 = input.nextInt();        System.out.println("How long would you like the finance to be?");         System.out.println("12 Months, 24 Months, 36 Months, 48 Months");        System.out.println("Please type either 12, 24, 36 etc");         int value2 = 0;        value2 = input.nextInt();        System.out.println("You will be paying " + value1 / value2 + " pounds a month!"); }        else if(value.equalsIgnoreCase ("")){            System.out.println("Please only enter numbers.");        }        else {            System.out.println("Great - you can pay the car in full!");            chooseOption = true;         }我嘗試使用parseInt,但是我只希望在輸入數字時發生parseInt。我希望我的程序能夠識別用戶輸入是否是整數,然后執行if/else語句,該語句使用該整數進行計算,如果輸入不是整數,那么我希望彈出一條消息,說“請確保您輸入數字”。更新我已經添加了某人在評論中建議的方法,但我不確定如何將其與我擁有的代碼相匹配,因為它仍然告訴我值不是整數,因此我無法使用“<”。
查看完整描述

3 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

歡迎來到堆棧溢出。


您正好在正確的路徑上,但您需要了解異常 - 當parseInt嘗試解析不是整數的值時,它會引發異常。在Java中,我們可以捕獲和異常并處理它(而不是讓它殺死程序的運行)。


對于您的情況,它看起來像這樣:


try {

  int result = Integer.parseInt(value);           

  //Do your normal stuff as result is valid

}

catch (NumberFormatException e)

{

   // Show your message "Please only enter numbers"  

}

//Code continues from here in either case


查看完整回答
反對 回復 2022-09-07
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

來自以下文檔:java.lang.Integer


Throws:

NumberFormatException - 如果字符串不包含可解析整數。


因此,只需捕獲該異常,您就會知道字符串不包含可解析整數。


int result;

try {

    result = Integer.parseInt(value);

}

catch (NumberFormatException e) {

    // value was not valid, handle here

}


查看完整回答
反對 回復 2022-09-07
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

您可以使用StringUtils.isNumeric,如果你想添加一個第三方庫。


如果你的用例足夠簡單,我可能會這樣做:


int intValue;

String value = "";

value = input.nextLine(); 

try {

    intValue = Integer.valueOf(value);

} catch (NumberFormatException e) {

    System.out.println("Please print only numbers!");

}


查看完整回答
反對 回復 2022-09-07
  • 3 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號