gpt4 book ai didi

Java连续计算coinFlip正面,如何突出显示连胜事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:08 24 4
gpt4 key购买 nike

好的,我的代码可以完成手头的任务。任务是翻转从单独的 Coin 类(此处未显示)实例化的 Coin 对象。我已经正确编写了代码,以便计算导致正面朝上的连续翻转的最大条纹作为输出。我想知道如何突出显示此连胜,所以当我在控制台中查看输出时,连胜是可见的,因为很难注意到 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);
}
}

最佳答案

我不是 100% 确定您所说的“突出显示”是什么意思。如果你只是想让它更引人注目,你总是可以在数字前打印几个 *。如果您使用的是 Eclipse,实际更改文本颜色的最简单方法是打印出您希望使用 System.err.println(outputToHighlight) 突出显示的代码。它会打印出红色。这是错误消息通常打印到控制台的方式。不过这只适用于 Eclipse。

也许解决问题的更好方法是打印出更少的抛硬币!

关于Java连续计算coinFlip正面,如何突出显示连胜事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15351619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com