Java 如何计算抛硬币的连续正面数
我需要帮助数一数连续出现的人头数。我不知道该怎么做。有谁能告诉我最快最简单的方法来完成这个项目吗?我似乎想不出来,我已经想了一段时间了。任何帮助都将不胜感激Java 如何计算抛硬币的连续正面数,java,Java,我需要帮助数一数连续出现的人头数。我不知道该怎么做。有谁能告诉我最快最简单的方法来完成这个项目吗?我似乎想不出来,我已经想了一段时间了。任何帮助都将不胜感激 import java.util.Scanner; import java.util.Random; public class test2 { public static void main( String[] args ) { Random randomNumber = new Random();
import java.util.Scanner;
import java.util.Random;
public class test2
{
public static void main( String[] args )
{
Random randomNumber = new Random();
//declares and initializes the headCount and consecutiveHeads
int headCount = 0;
int consecutiveHeads = 0;
//Ask user how many coin flips
System.out.println( "How many coin flips?" );
//stores the input in coinFlips
Scanner input = new Scanner( System.in );
int coinFlips = input.nextInt();
//loop makes sure that program only accepts values or 1 or greater
while (coinFlips < 1)
{
System.out.println("Please enter a value greater than or equal to 1.");
coinFlips = input.nextInt();
}
for (int i = 0; i < coinFlips; i++)
{
int coinFace = randomNumber.nextInt(2);
if (1 == coinFace)
{
//Print H for heads and increment the headCount
System.out.print( "H" );
headCount++;
}
else
{
//print T for tails
System.out.print( "T" );
}
}
}
}
import java.util.Scanner;
导入java.util.Random;
公共类test2
{
公共静态void main(字符串[]args)
{
随机数=新随机数();
//声明并初始化人数和连续人数
内部人员编制=0;
int连续头=0;
//询问用户掷多少硬币
System.out.println(“投币多少?”);
//将输入存储在coinFlips中
扫描仪输入=新扫描仪(System.in);
int coinFlips=input.nextInt();
//循环确保程序只接受1或更大的值
while(coinFlips<1)
{
System.out.println(“请输入大于或等于1的值”);
coinFlips=input.nextInt();
}
for(int i=0;i
我想说的最简单的方法就是在if子句的heads侧计算连续的head,并在Tails侧将其设置为零。像这样:
[...]
if (1 == coinFace)
{
//Print H for heads and increment the headCount
System.out.print( "H" );
headCount++;
consecutiveHeads++;
}
else
{
//print T for tails
System.out.print( "T" );
consecutiveHeads = 0;
// if current head count is greater than previously recorded maximum count, replace old max
}
如果您想记住最高的连续计数,您可能需要为此添加一个变量。因此,上述内容变成:
if (1 == coinFace)
{
//Print H for heads and increment the headCount
System.out.print( "H" );
headCount++;
consecutiveHeads++;
if(consecutiveHeads > longestHeadStreak)
{
longestHeadStreak = consecutiveHeads;
}
}
我想说的最简单的方法就是在if子句的heads端计算连续的head,并在Tails端将其设置为零。像这样:
[...]
if (1 == coinFace)
{
//Print H for heads and increment the headCount
System.out.print( "H" );
headCount++;
consecutiveHeads++;
}
else
{
//print T for tails
System.out.print( "T" );
consecutiveHeads = 0;
// if current head count is greater than previously recorded maximum count, replace old max
}
如果您想记住最高的连续计数,您可能需要为此添加一个变量。因此,上述内容变成:
if (1 == coinFace)
{
//Print H for heads and increment the headCount
System.out.print( "H" );
headCount++;
consecutiveHeads++;
if(consecutiveHeads > longestHeadStreak)
{
longestHeadStreak = consecutiveHeads;
}
}
但我不确定这是怎么回事。如果我在If子句中增加连续的头数,那么它本质上不是与员工总数相同吗?不,因为一旦得到尾数,就将连续头数设置为零。您永远不会将员工人数设置为零,只需将其递增即可。看看else-子句。我是初始化longestHeadStreak=1还是0?nvm,我知道了。谢谢你的帮助!但我不确定这是怎么回事。如果我在If子句中增加连续的头数,那么它本质上不是与员工总数相同吗?不,因为一旦得到尾数,就将连续头数设置为零。您永远不会将员工人数设置为零,只需将其递增即可。看看else-子句。我是初始化longestHeadStreak=1还是0?nvm,我知道了。谢谢你的帮助!