Solidity

從映射中增加唯一的返回值

  • March 1, 2022

假設我有一個名為 Tries 的映射。每次呼叫函式時,我想根據嘗試次數(嘗試呼叫函式)將返回值從 0 增加到 x。這是映射:

mapping(uint256 => uint256) public Tries;

這是功能:

function Unlock(uint256 tokenId, string memory _answer) public {
       require(ownerOf(tokenId) == msg.sender, "you are not the owner!");
       Tries[tokenId] = ???
}

你可以這樣試試:

function Unlock(uint256 tokenId) public {
       require(ownerOf(tokenId) == msg.sender, "you are not the owner!");
       uint x = Tries[tokenId];
       Tries[tokenId] = x+1;
}

您可以使用以下程式碼行從映射中恢復嘗試的最後一個值:int x = Tries[tokenId]。然後為每次嘗試增加目前值**+1 (或另一個數字)。**

引用自:https://ethereum.stackexchange.com/questions/122840