2017-06-14 39 views
-6

我是低級別的Java用戶,現在我正在嘗試學習Lua ......並且我真的不明白,寫出來的東西讓我更加困惑。如果有人能夠將這篇短篇文章從Java轉換爲Lua,那麼我將能夠比較它們並找出與之相當的內容。我會非常感激。那麼請幫忙,好嗎?將Java轉換爲Lua

public String IllumByDay(double cycle) { 
     DecimalFormat myFormatter = new DecimalFormat("###.##"); 

     for (double dayCounter = 0; dayCounter <= cycle - 1; dayCounter++) { 
      double illum = 0.5 * (1 + Math.sin(2 * Math.PI * (dayCounter/cycle - 0.25))); 
      String roundedIllum = myFormatter.format(illum * 100); 
      System.out.println("Day " + (int) (dayCounter + 1) + ": " + roundedIllum + "%"); 
     } 
    } 
+4

歡迎堆棧溢出。這不是一個代碼翻譯服務。 Lua語法非常簡單。你太懶了,希望有人會提供給你解決方案...請閱讀[問] – Piglet

+2

我投票結束這個問題,因爲代碼翻譯請求是題外話 – Piglet

+0

哪個部分讓你感到困惑?不可能是全部,因爲如果一個普通的'for'循環令人困惑,那麼在嘗試這樣的事情之前,你有很多東西要學習。另外,希望'+',''''''和'/'等操作符也不會讓人困惑。 – Andreas

回答

1

讓我們來幫忙,呃?

你可以像這樣的東西開始:

function IllumByDay(cycle) 
    for dayCounter = 0, cycle-1 do 
     local illum = 0.5 * (1.0 + math.sin(2.0*math.pi*(dayCounter/cycle - 0.25))) 
     local roundedIllum = string.format('%.2f', illum * 100.0) 
     print(string.format("Day %d: %s%", dayCounter+1, roundedIllum)) 
    end 
end