forLoop, WhileLoop
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ArtAuction {
mapping(uint => uint) public artworks;
uint public constant NUM_ARTWORKS = 5;
constructor() {
// 初始化畫作和價格
for (uint i = 0; i < NUM_ARTWORKS; i++) {
artworks[i] = (i + 1) * 100; // 價格為畫作編號乘以 100
}
}
function buyArtworks(uint[] memory _artworkIds) public payable {
require(_artworkIds.length > 0, "At least one artwork must be purchased");
uint totalCost = 0;
for (uint i = 0; i < _artworkIds.length; i++) {
uint artworkId = _artworkIds[i];
require(artworkId < NUM_ARTWORKS, "Invalid artwork ID");
totalCost += artworks[artworkId];
artworks[artworkId] = 0; // 購買後將畫作庫存設為 0
}
require(msg.value >= totalCost, "Insufficient funds");
// 購買成功,執行轉帳等操作
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ArtAuction {
mapping(uint => uint) public artworks;
uint public constant NUM_ARTWORKS = 5;
constructor() {
// 初始化畫作和價格
uint i = 0;
while (i < NUM_ARTWORKS) {
artworks[i] = (i + 1) * 100; // 價格為畫作編號乘以 100
i++;
}
}
function buyArtworks(uint[] memory _artworkIds) public payable {
require(_artworkIds.length > 0, "At least one artwork must be purchased");
uint totalCost = 0;
uint i = 0;
while (i < _artworkIds.length) {
uint artworkId = _artworkIds[i];
require(artworkId < NUM_ARTWORKS, "Invalid artwork ID");
totalCost += artworks[artworkId];
artworks[artworkId] = 0; // 購買後將畫作庫存設為 0
i++;
}
require(msg.value >= totalCost, "Insufficient funds");
// 購買成功,執行轉帳等操作
}
}
ArtAuction
用於小朋友的義賣活動。for
迴圈和 while
迴圈都用於初始化畫作和價格,以及在購買畫作時遍歷 _artworkIds
陣列。// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
contract loop {
uint public a = 10;
function ForLoop() public {
for(uint i = 0; i < 10; i++) {
a++;
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
contract oddNum {
function isOddNumber(uint _number) public view returns(bool) {
return (_number % 2 != 0 ? true : false);
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
contract accountBalance {
uint amount = 0;
uint public Profit = 0;
function sell() public {
amount++;
}
function profit() public {
Profit = amount * 3000 * (10 **18);
}
}
function sell() public { ... }
: 此函式名為 sell
,是一個公開(public)的函式,沒有參數。每次執行此函式,amount
變數的值會增加 1,代表賣出了一斤水果。function profit() public { ... }
: 此函式名為 profit
,是一個公開(public)的函式,沒有參數。當執行此函式時,會計算當前的利潤,利潤的計算方式是將 amount
變數乘以 3000(代表一斤水果的價格,以 wei 為單位)再乘以 10 的 18 次方(將結果轉換為 wei)。最後,將計算結果存儲在 Profit
變數中// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
contract String {
int public answer = 10;
function Operator(string memory operation) public {
if(keccak256(abi.encodePacked(operation)) == keccak256(abi.encodePacked("add"))){
answer = 1+1;
}
if(keccak256(abi.encodePacked(operation)) == keccak256(abi.encodePacked("sub"))){
answer = 1-1;
}
}
}
string memory operation
。這個參數是函數的輸入,用於指定要進行的數學運算。memory
關鍵字表示這個參數是在內存中分配的,不會影響合約的永久存儲。keccak256(abi.encodePacked(operation))
來對輸入的操作進行哈希處理,以確保無論操作是大寫還是小寫,都能正確識別。這裡使用了keccak256
函數來生成哈希值,因為它是Solidity中可用的哈希函數之一。