[英]boolean not holding value when method is executed
我有一種遇到問題的方法。 下面的第二個方法hintForForPinNumber()調用第一個方法canConvertToInteger(),然后根據布爾變量pinValid的值是true還是false來執行操作。
當我自己執行方法canConvertToInteger()時,它的功能很好,並且pinValid的值正確。
當我執行hintForForPinNumber()並輸入一個引發異常的字符串時,pinValid的值保持為true,因此if else塊的else部分未執行,但是pinTry的值為0,因此必須有一個異常已經被抓到並處理了。 那么為什么當pinValid的布爾值應該為false時才為true?
應該發生的情況是,如果在OUDialog.request框中輸入了無效的條目,則應將pinValid設置為false,然后應將pinTry的值更改為0,並且
public boolean canConvertToInteger()
{
String pinAttempt;
{
pinAttempt = OUDialog.request("Enter your pin number");
try
{
this.pinTry=Integer.parseInt(pinAttempt);
this.pinValid = true;
}
catch (NumberFormatException anException)
{
this.pinTry=0;
this.pinValid = false;
}
}
return this.pinValid;
}
public int promptForPinNumber()
{
this.canConvertToInteger();
if (pinValid = true)
{
return this.pinTry;
}
else
{
OUDialog.alert("Number entered is not a valid pin");
return this.pinTry;
}
}
經典一,更換
if (pinValid = true)
與:
if (pinValid == true)
甚至更好:
if (pinValid)
pinValid = 1
是賦值 ,而不是表達式(條件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.