Replies: 25 comments 8 replies
|
Cliff Lee CL (程式、專案管理) 我的答案public class Question1
{
const float CRITICAL_MULTIPLIER = 1.5f;
/// <summary>
/// Calculate the atk if critical occured or not
/// </summary>
/// <param name="atk">The attack point</param>
/// <param name="critRate">The critical rate</param>
/// <param name="rand">The random value</param>
/// <returns>Modified attack point</returns>
public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk <= 0)
return 0;
if (critRate > rand)
return (int)(atk * CRITICAL_MULTIPLIER);
return atk;
}
} |
0 replies
|
JIA(程式新手) 我的答案public int CriticalAtk(int atk, float critRate, float rand)
{
bool isAtkLessThenZero = atk <= 0;
if (isAtkLessThenZero) return 0;
const float criticalHitMultiplier = 1.5f;
bool isCritRateGreaterThanZero = critRate > 0;
bool isCritRate = rand <= critRate;
if (isCritRate && isCritRateGreaterThanZero) return (int)(atk * criticalHitMultiplier);
return atk;
} |
0 replies
|
TWEdward(新人/程式) 我的答案public int CriticalAtk(int atk, float critRate, float rand)
{
//throw new NotImplementedException("code something here ...");
if(atk < 0)
{
return 0;
}
if(rand < critRate) //
{
return (int)(atk * 1.5);
}
else
{
return atk;
}
}第一次碰到單元測試,測試時過不了程式碼再重寫一次,第二次過不了再再重寫一次,第三次過不了再再再重寫一次,終於給它過了 |
1 reply
|
riverRobot (新人/企劃/程式) 我的答案public class Question1
{
/// <summary>
/// Calculate the atk if critical occured or not
/// </summary>
/// <param name="atk">The attack point</param>
/// <param name="critRate">The critical rate</param>
/// <param name="rand">The random value</param>
/// <returns>Modified attack point</returns>
public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk < 0)
return 0;
if(critRate > rand)
return (int)(atk * 1.75f);
return atk;
}
}課程/作業心得好耶爆擊 |
0 replies
|
肥羊(程式) 我的答案public class Question1
{
const float critRateMultiplier = 1.5f;
/// <summary>
/// Calculate the atk if critical occured or not
/// </summary>
/// <param name="atk">The attack point</param>
/// <param name="critRate">The critical rate</param>
/// <param name="rand">The random value</param>
/// <returns>Modified attack point</returns>
public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk <= 0) return 0;
if (critRate > rand)
{
return (int)(atk * critRateMultiplier);
}
else
{
return atk;
}
}
} |
0 replies
|
ChiaBurn(程式 | 企劃) 作業程式碼public int CriticalAtk(int atk, float critRate, float rand)
{
// 如果atk小於0,回傳0
if (atk <= 0)
{
return 0;
}
// 若爆擊率大於隨機值,代表爆擊成功。(爆擊率越高越容易發生爆擊效果)
bool isCriticalSuccess = critRate > rand;
if (isCriticalSuccess)
{
// 爆擊時攻擊力會變為基礎攻擊力的1.5倍,回傳結果指定為整數。
return (int)(atk * 1.5);
}
return atk;
}想法與反饋之前寫作業時我是利用LinQPad執行程式,並另外寫了for迴圈來產生隨機input後call CriticalAtk進行測試: // 進行5次測試
for(int i = 0; i <5; i++)
{
Random randGenerator = new Random();
// 基礎攻擊力:隨機取-1~5之間的整數
int atk = randGenerator.Next(-5, 10);
// 爆擊率:介於0~1之間的浮點數(0~100%)
float critRate = (float)randGenerator.Next(0, 100) / 100;
// 隨機值:介於0~1之間的浮點數(0~100%)。
float rand = (float)randGenerator.Next(0, 100) / 100;
$"atk = {atk}, critRate = {critRate}, rand = {rand},爆擊攻擊力計算結果為:".Dump();
CriticalAtk(atk, critRate, rand).Dump();
}
這樣的測試方式有幾個不方便的地方:
相比之下寫unit test來跑真的方便許多! |
1 reply
|
Tina( 程式 ) 我的答案using System;
namespace PG0001.Questions
{
public class Question1
{
/// <summary>
/// Calculate the atk if critical occured or not
/// </summary>
/// <param name="atk">The attack point</param>
/// <param name="critRate">The critical rate</param>
/// <param name="rand">The random value</param>
/// <returns>Modified attack point</returns>
public int CriticalAtk(int atk, float critRate, float rand)
{
int result = 0;
if(atk < 0){
Console.WriteLine("Wrong atk value.");
return result;
}
result = atk;
if(critRate > rand){
Console.WriteLine("Have cirtical.");
result = (int) (result * 1.5f);
}
else
Console.WriteLine("Don't have cirtical.");
return result;
}
}
} |
0 replies
|
芋圓(美術/新人) 我的答案public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk>0)
{
if(rand>=critRate) //隨機值高於等於暴擊率=>沒有暴擊
{
atk=atk*1;
}else if(rand<critRate) //隨機值小於暴擊率=>判定暴擊
{
atk=(int)(atk*1.5);
}
}
else if(atk<=0)
{
return 0;
}
return atk;
}想法與反饋第一次測試是使用上次寫過的答案 但在NoCrit的單元測試那出了點問題 後來看了測試文件的程式碼後 更改了隨機值與暴率間的判定關係就通過測試了 不過有點好奇為甚麼兩者相等時是要判定成沒有暴擊? |
1 reply
|
七七(程式) 我的答案public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk < 0)
return 0;
// if (rand >= critRate)
if (rand < critRate)
{
return atk = (int)(atk * 1.5);
}
return atk;
}回頭補作業 ‹‹( ˙▿˙ )/››‹‹( ˙▿˙ )/›› |
0 replies
|
Dust(程式) 回答疑問: |
2 replies
|
小鲨 (程式) 答案public class Question1
{
/// <summary>
/// Calculate the atk if critical occured or not
/// </summary>
/// <param name="atk">The attack point</param>
/// <param name="critRate">The critical rate</param>
/// <param name="rand">The random value</param>
/// <returns>Modified attack point</returns>
public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk <= 0)
{
return 0;
}
return rand < critRate ? (int)(atk * 1.5f) : atk;
}
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment



























Uh oh!
There was an error while loading. Please reload this page.
Introduction
寫出一個計算爆擊的函式
Answer
解答
需要注意的是爆擊率如果等於0,理論上不會有任何可能發生爆擊,
但在產出rand的時候可能有0的值,所以題目假設區間為[0, 1) (不等式0 <= rand < x),
因此沒有寫rand <= critRate
All reactions