【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-BreakTest.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
import java.io.*;
public class BreakTest
{
private short[] values;
private final int NUM_VALUES = 100000;
public BreakTest()
{
java.util.Random random = new java.util.Random();
values = new short[NUM_VALUES];
for(int i = 0; i < NUM_VALUES; i++)
{
values[i] = (short)Math.abs(random.nextInt()%2000);
}
}
public int calculateBonusA(int minScore, int bonus)
{
int pointsEarned = 0;
for(int i = 0; i < values.length; i++)
{
if(values[i] >= minScore)
{
pointsEarned += bonus;
}
}
return pointsEarned;
}
public int calculateBonusB(int minScore, int bonus)
{
int pointsEarned = 0;
java.util.Arrays.sort(values);
for(int i = values.length-1; i >= 0 ; i--)
{
if(values[i] >= minScore)
{
pointsEarned += bonus;
}
else break;
}
return pointsEarned;
}
public static void main(String[] args)
{
BreakTest bt = new BreakTest();
System.out.println(bt.calculateBonusA(500, 10));
System.out.println(bt.calculateBonusB(500, 10));
} // main
} // BreakTest