好的,所以我的代码适用于手头的任务。赋值是翻转从单独的Coin类(此处未显示)实例化的Coin对象。我已正确编写代码,以便计算连续翻转的最大条纹,从而导致Heads作为输出。我想知道我怎么可能突出这条连线所以当我在控制台中查看输出时条纹是可见的,因为很难注意到100翻转列表中的条纹。
这是我的代码:
public class Runs
{
public static void main (String[] args)
{
final int FLIPS = 100; // number of coin flips
int currentRun =0; // length of the current run of HEADS
int maxRun =0; // length of the maximum run so far
// Create a coin objecti
Coin coin = new Coin();
// Flip the coin FLIPS times
for (int i = 0; i < FLIPS; i++)
{
// Flip the coin & print the result
coin.flip();
int flipCount = i + 1;
System.out.println("Flip " + flipCount +":"+ " " + coin.toString());
// Update the run information
if (coin.isHeads()==true)
{
if (maxRun<currentRun)
{
maxRun=currentRun;
}
currentRun+=1;
}
else
{
currentRun = 0;
}
}
// Print the results
System.out.println("Maximum run of heads in a row! : " + maxRun);
}
}
答案 0 :(得分:1)
我不是100%肯定“亮点”是什么意思。如果您只是想让它更明显,您可以在数字前打印几个*。如果您正在使用Eclipse,实际更改文本颜色的最简单方法是使用System.err.println(outputToHighlight)
打印出您想要突出显示的代码。它将打印出红色。这是错误消息通常打印到控制台的方式。这只适用于Eclipse。
或许解决问题的更好方法是打印出更少的硬币翻转!
答案 1 :(得分:1)
不是“突出显示”可能是设备/操作系统特定的输出,而是输出发生的迷你报告以及它的持续时间。
以下是代码的外观(我已经为您简化了 - 请参阅代码中的注释):
int maxRun = 0;
int currentRun = 0;
int runStart = 0;
for (int i = 0; i < FLIPS; i++) {
coin.flip();
System.out.println("Flip " + (i+1) +": " + coin); // toString() is redundant
if (coin.isHeads()) { // never compare a boolean with a boolean constant, just use it
currentRun++; // use ++ in preference to +=1, and this should be before maxRun test
if (maxRun < currentRun) {
maxRun = currentRun;
runStart = currentRun - i; // this will produce a 1-based position
}
} else {
currentRun = 0;
}
}
System.out.println("Largest run was " + maxRun + " long, starting at " + runStart);