[英]Programming on Solidity setName function (BitDegree)
作为 Bitdegree 学习稳固性课程的一部分。 我被要求做以下事情:
2.创建一个名为 increaseCounter 的新函数,它会在每次调用时将 counter 的值增加 10
我已经尝试了多种方法来设置名称,但遇到了很多问题,如果有人可以帮助我解决这个问题,我将不胜感激,它是如此基本但由于某种原因没有任何工作:(。这是当前代码
pragma solidity ^0.4.16;
contract FunctionTest {
bool public foo = true;
string public name;
uint256 public counter = 0;
function setName() public {
//
}
function writeToStorage() {
foo = !foo;
}
function readFromStorageConstant() public constant returns (bool) {
return foo;
}
function readFromStorageView() public view returns (bool) {
return foo;
}
}
出于某种原因,它需要将变量“名称”初始化为字符串以使其提交。
pragma solidity ^0.4.16;
contract FunctionTest {
bool public foo = true;
string public name="ChuckNorris";
uint256 public counter = 0;
function setName(string _name) public {
name = _name;
}
function increaseCounter() public {
counter += 10;
}
function writeToStorage() {
foo = !foo;
}
function readFromStorageConstant() public constant returns (bool) {
return foo;
}
function readFromStorageView() public view returns (bool) {
return foo;
}
}
确保您回答了问题的两个部分。 出于某种原因,在本课中,setName() 函数在您完成 increaseCounter() 函数之前不会显示您已正确完成。这是帮助我进入下一个级别的解决方案。
pragma solidity ^0.4.16;
contract FunctionTest {
bool public foo = true;
string public name;
uint256 public counter = 0;
function setName(string _name) public {
name = _name;
}
function increaseCounter() public {
counter += 10;
}
function writeToStorage() {
foo = !foo;
}
function readFromStorageConstant() public constant returns (bool) {
return foo;
}
function readFromStorageView() public view returns (bool) {
return foo;
}
funciton increaseCounter() {
counter += 10; }
}
应该很简单...
pragma solidity ^0.4.16;
contract FunctionTest {
bool public foo = true;
string public name;
uint256 public counter = 0;
function setName(string _name) public {
name = _name;
}
function increaseCounter() public {
counter += 10;
}
function writeToStorage() {
foo = !foo;
}
function readFromStorageConstant() public constant returns (bool) {
return foo;
}
function readFromStorageView() public view returns (bool) {
return foo;
}
}
// SPDX-License-Identifier: nolicense
pragma solidity ^0.8.0;
contract sample{
string public name;
function setName(string memory _newName) public returns(bool){
name =_newName;
return true;
}
function getName() public view returns (string memory){
return name;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.