使用 parseInt 改变一个整数的值

change value of an integer using parseInt

所以我正在编写一个方法,该方法应该提示用户以字符串形式输入他们的 PIN 码,然后将其转换为 int(或不取决于它是否抛出异常),这是我需要的分配给 int pinNumber.

我遇到的问题是,新对象在创建时由构造函数分配了一个 pin 编号,并且在执行以下方法时此值未更改。我错过了什么?

   public boolean canConvertToInteger()
   {
      boolean result = false;
      String pinAttempt;
      {
         pinAttempt = OUDialog.request("Enter your pin number");
         try
         {
            int pinNumber = Integer.parseInt(pinAttempt);
            return true;
         }
          catch (NumberFormatException anException)
         {
            return false;
         }
      }

编辑:将 pinAttempt 更改为 pinNumber(拼写错误)

看看这个街区

try
{
   int pinNumber = Integer.parseInt(pinAttempt);
   return true;
}

pinNumber 只会在 try 块的范围内具有您期望的值。

我想你想做

try
{
   this.pinNumber = Integer.parseInt(pinAttempt);
   return true;
}

相反。