Solidity setName函数编程(BitDegree)

时间:2018-01-21 01:24:57

标签: function blockchain ethereum solidity

作为Bitdegree学习坚固课程的一部分。我被要求做以下事情:

  1. 以允许设置名称
  2. 的值的方式修改函数setName

    2.创建一个名为increaseCounter的新函数,只要调用它就会将计数器的值增加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;
    }
    

    }

3 个答案:

答案 0 :(得分:1)

出于某种原因,它需要变量' name'要初始化为字符串以使其提交。

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;
  }
}

答案 1 :(得分:1)

确保您同时回答了问题的两个部分。由于本课程中的某些原因,setName()函数将不会显示您已经正确完成了该函数,直到您完成了gainCounter()函数。以下是解决方案,可以帮助我进一步发展。

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; } 
}

答案 2 :(得分:0)

应该非常简单......

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;
  }
}