Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Eclipse中投币Java的设置_Java - Fatal编程技术网

Eclipse中投币Java的设置

Eclipse中投币Java的设置,java,Java,我需要帮助在eclipse中使用java启动抛硬币代码,该代码需要包含硬币上的用户输入,并在代码启动抛硬币时调用硬币 以下是我目前掌握的代码: 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(); 但是我不知道下一步

我需要帮助在eclipse中使用java启动抛硬币代码,该代码需要包含硬币上的用户输入,并在代码启动抛硬币时调用硬币

以下是我目前掌握的代码:

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();

但是我不知道下一步该怎么办。

我不明白你的问题,但是: -正常启动、类、主方法等 -制造一枚可变整数的硬币等 -使用import java.util.Scanner和int x=Scanner.nextInt获取用户输入的内容? -使用math.random进行随机掷骰,可能0.5+是头 -你得到了什么=硬币
-优化您的问题

您必须生成一个随机数0或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");

}

到目前为止您尝试了什么?好的,您有用户输入。接下来是生成一个随机数来抛硬币?你可以用Math.random,或者使用随机类。如果你在这里搜索“java投币”,你会发现许多类似的问题和答案,可能会给你一些有用的想法。我需要用户输入猜测投币时间。在代码区域中,如果选择>1,则头需要为1,尾需要为0。如果选择>1,则不会导致代码停止,因为tails是0,如果调用0或tails,它会停止代码并说0是无效的input@Carlos为什么它会在条件选择>1中的0处停止,原因是我提出的条件是您将用户输入限制为仅0或1我只是问我不理解代码的那部分,您现在理解了吗。