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;
}
} |
Beta Was this translation helpful? Give feedback.
-
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;
} |
Beta Was this translation helpful? Give feedback.
-
Syuan(新人) 我的答案 public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk <= 0) return 0;
return critRate > rand ? (int)(atk * 1.5) : atk;
} |
Beta Was this translation helpful? Give feedback.
-
罐頭(新人) 我的答案ヾ(•ω•`)opublic int CriticalAtk(int atk, float critRate, float rand)
{
if (atk >= 0)
{
if (rand < critRate)
return (int)(atk * 1.5f);
return atk;
}
return 0;
} 連假結束了,我被爆擊了இ௰இ |
Beta Was this translation helpful? Give feedback.
-
吉米(不知道要學什麼但是喜歡創作的新人) 我的答案在這 public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk > 0)
return critRate > rand ? (int)(atk * 1.5) : atk;
else
return 0;
} 我.需要..更多的..知識! |
Beta Was this translation helpful? Give feedback.
-
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;
}
} 第一次碰到單元測試,測試時過不了程式碼再重寫一次,第二次過不了再再重寫一次,第三次過不了再再再重寫一次,終於給它過了 |
Beta Was this translation helpful? Give feedback.
-
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;
}
} 課程/作業心得好耶爆擊 |
Beta Was this translation helpful? Give feedback.
-
小4 ( 程式 ) public int CriticalAtk(int atk, float critRate, float rand)
{
atk = atk <= 0 ? 0 : atk;
bool isCrit = critRate > rand;
atk = isCrit ? (int)(atk * 1.5f) : atk;
return atk;
} |
Beta Was this translation helpful? Give feedback.
-
肥羊(程式) 我的答案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;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Suncacao(程式) 我的答案public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk < 0){
return 0;
}else if(critRate > rand){
return (int)(atk * 1.5f);
}
return atk;
} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
老蕭OLDShaw(程式) 我的答案 public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk < 0) return 0;
if(rand < critRate)
{
atk = (int)(atk * 1.5f);
}
return atk;
} |
Beta Was this translation helpful? Give feedback.
-
Snoweve (程式) 我的答案 public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk <= 0) return 0;
return critRate > rand ? (int)(atk * 1.5) : atk;
} <<反饋>> |
Beta Was this translation helpful? Give feedback.
-
肉鬆(程式 / 音樂) 我的答案 public int CriticalAtk(int atk, float critRate, float rand)
{
if (atk <= 0)
return 0;
else if (rand < critRate)
return (int)(atk * 1.5f);
return atk;
} 勞贖 |
Beta Was this translation helpful? Give feedback.
-
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來跑真的方便許多! |
Beta Was this translation helpful? Give feedback.
-
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;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
阿邦(企劃) 我的解答public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk <= 0) return 0;
if(rand < critRate) return (int)(atk*1.5f);
else return atk;
} 謝謝老師指教! |
Beta Was this translation helpful? Give feedback.
-
芋圓(美術/新人) 我的答案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的單元測試那出了點問題 後來看了測試文件的程式碼後 更改了隨機值與暴率間的判定關係就通過測試了 不過有點好奇為甚麼兩者相等時是要判定成沒有暴擊? |
Beta Was this translation helpful? Give feedback.
-
JSheng(程式) 我的答案public int CriticalAtk(int atk, float critRate, float rand)
{
float value = atk;
if(rand < critRate)
value *= 1.5f;
return (int)Mathf.Max(0, value);
} |
Beta Was this translation helpful? Give feedback.
-
鮪魚腹排(新人) 反饋快鼠了,感覺我好像前面打了一些多餘的程式OWQ |
Beta Was this translation helpful? Give feedback.
-
七七(程式) 我的答案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;
} 回頭補作業 ‹‹( ˙▿˙ )/››‹‹( ˙▿˙ )/›› |
Beta Was this translation helpful? Give feedback.
-
Dust(程式) 回答
疑問: |
Beta Was this translation helpful? Give feedback.
-
小鲨 (程式) 答案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;
}
} |
Beta Was this translation helpful? Give feedback.
-
歐雷(程式) 答案 public int CriticalAtk(int atk, float critRate, float rand)
{
if(atk < 0) return atk = 0;
if(critRate > rand) atk = (int)(atk * 1.5f);
return atk;
} |
Beta Was this translation helpful? Give feedback.
-
極光(程式) 我的答案 public int CriticalAtk(int atk, float critRate, float rand)
{
if (0 > atk)
{
return 0;
}
if (critRate>rand)
{
return (int)(atk * 1.5f);
}
return atk;
} |
Beta Was this translation helpful? Give feedback.
-
Introduction
寫出一個計算爆擊的函式
Answer
解答
需要注意的是爆擊率如果等於0,理論上不會有任何可能發生爆擊,
但在產出rand的時候可能有0的值,所以題目假設區間為[0, 1) (不等式0 <= rand < x),
因此沒有寫rand <= critRate
Beta Was this translation helpful? Give feedback.
All reactions