我有这段代码,它允许我模拟抛硬币,0 表示正面,1 表示反面或任何您想要解释的。当您运行该程序时,它会随机生成(在本例中)两次抛硬币的 10 种组合。我想做的是修改这个程序,让用户可以询问抛硬币的次数,显示硬币结果,然后可以提示他再次抛硬币。
public class Dice {
public static void main(String[] args)
{
for (int i = 0; i <= 10; i++)
{
int benito1=(int)(Math.random()*2);
int benito2=(int)(Math.random()*2);
System.out.println(benito1 + " " +benito2);
}
System.out.println();
}
}
最佳答案
像这样:
Scanner sc = new Scanner(System.in);
System.out.prinltn("Please enter a number");
int input = sc.nextInt();
while(input-->0)
{
int benito1=(int)(Math.random()*2);
int benito2=(int)(Math.random()*2);
System.out.println(benito1 + " " +benito2);
}
关于java - 我怎样才能根据用户输入使这个抛硬币模拟工作? eclipse ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21771911/