Eclipse中硬币翻转Java的设置



我需要帮助使用Java在Eclipse中折腾的代码时,该代码需要包含硬币上的用户输入,并在代码启动硬币扔掉硬币后调用硬币。

这是我到目前为止的代码:

Scanner input = new Scanner(System.in);
int choice;
int heads = 1;
int tails = 0;
System.out.print("Please call the toss, heads or tails: ");
choice = input.nextInt();

但我不确定下一步该怎么做。

您必须生成一个随机数0 or 1,然后与用户输入进行比较,然后根据比较进行其余的。

Scanner input = new Scanner(System.in);
int choice;
System.out.print("Please call the toss, heads(1) or tails(0): ");
choice = input.nextInt();
int randomNum = Random.nextInt(2) + 1;
if(randomNum == choice){
     System.out.print("You win.");
}
else{
     System.out.print("You lost.");
}

您可以做这样的事情。

Scanner input = new Scanner(System.in);
int choice;
int heads = 1;
int tails = 0;
System.out.print("Please call the toss, heads or tails: ");
choice = input.nextInt();
if(choice > 1)
{
   System.out.println("Invalid choice");
}
//Get the random number either between 0 or 1 
int randomNum = ThreadLocalRandom.current().nextInt(tails, heads + 1);
if(randomNum  == choice)
{
   System.out.println("You win");
}
else
{
   System.out.println("You lose");
}

我没有得到您的问题,但是: - 正常启动(类,主要方法等) - 制作变量(int币等) - 使用导入java.util.scanner和int x = scanner.nextint()获取用户输入(什么?) - 使用数学。随机折腾,可能是0.5 是头 - 你得到什么=硬币 - 您的问题

最新更新