gpt4 book ai didi

java - 将 'menu' 替换为用户的输入

转载 作者:太空宇宙 更新时间:2023-11-04 12:14:39 25 4
gpt4 key购买 nike

出于学习的兴趣,我决定编写一个抛硬币程序。硬币是一个枚举,我让程序返回该枚举值。我还通过菜单样式获得了用户输入,但这得益于我不久前购买的一本 Barnes and Nobles 书籍的帮助。

我想我已经来到了一个奇怪的十字路口。我想基本上返回枚举值等,但删除“菜单”方面,并将其替换为用户输入他们想要进行多少次翻转的能力,并且如果他们愿意的话也可以重复该程序(因此,不要每次输入20000时按1进行翻转,它会翻转很多次,我也认为这样做将有助于公平性检查,因为真正的公平性测试将返回几乎均匀数量的正面和反面,如果要翻转多次,然后按0表示不翻转会结束程序),我想提示用户并询问他们是否愿意重复。

这是我编写的程序:

import java.util.*;

public class CoinTossing
{
private enum Coin { HEADS, TAILS };

private static final Random randomNumbers = new Random();

private static final int HEAD = 1;
private static final int TAIL = 2;

public static void main(String[] args)
{
Scanner input = new Scanner( System.in );

int choice;
int toss = 0;
int tosses = 0;
int frontflip = 0;
int backflip = 0;

Coin gameStatus;

System.out.println("Welcome to the Coin Toss Program.\n");
System.out.println("Choose from the menu what you want to do.");
System.out.print("1. Toss the coin\n2. Quit program\n");
choice = input.nextInt();

while ( choice != 0 )
{
if ( choice == 1 )
{
int CoinTossed = Flip();

switch ( CoinTossed )
{
//added tosses to switch statement to make the counter work perfect.
case HEAD:
gameStatus = Coin.HEADS;
tosses++; // add amount of tosses
break;
default: // changed case TAIL to default. Easy and works.
gameStatus = Coin.TAILS;
tosses++; // add amount of tosses
break;
}

if ( gameStatus == Coin.HEADS )
{
frontflip++; //Add amount of heads
}
else // gameStatus == TAILS
backflip++; //Add amount of tails
}

// A try to make an real exit out of a program

if ( choice == 2 )
{
EndProgram( frontflip, backflip, tosses );
}

System.out.println("\nChoose from the menu what you want to do.");
System.out.print("1. Toss the coin\n2. Quit program\n");
choice = input.nextInt();
}
}

//Toss the coin to determine 1 or 2.
public static int Flip()
{
int toss;

toss = 1 + randomNumbers.nextInt( 2 );

if ( toss == 1 )
{
System.out.println("You toss the coin and it lands on head!");
}
else
{
System.out.println("You toss the coin and it lands on tail!");
}
return toss;
}

public static void EndProgram( int frontflip, int backflip, int tosses )
{
System.out.printf("You have tossed %d times.\n", tosses);
System.out.printf("Of all those tosses, %d landed on heads, and %d on tails.\n", frontflip, backflip);
System.exit(0);
}
}

我想我需要一个 do/while 循环,以便我可以让用户回答“你想再玩一次吗?”是或否的问题?在循环内部我有一个 switch 语句,它还表示如果用户输入 0 作为程序结束的翻转次数?

我想我可以添加此代码片段来获取输入:

System.out.println("How many flips do you want?");
System.out.println("(0 will exit the program)");
number = input.nextInt();

我正在考虑创建一个新变量并让用户设置抛掷次数。然后像这样复合 while 循环检查

while(choice != 0 && numTosses !=0)

然后减少计数,我将不得不检查该计数,一旦达到 0 打印结果,最多有多少个正面和多少个反面,然后提示用户是否想再次玩游戏,但我很难得到正确的结果。老实说,我什至不知道为什么我要尝试这样做,如果不是为了知识方面,所以如果你不想帮助布罗斯基,我理解。我觉得我走在正确的道路上。

最佳答案

您可以使用 2 个循环:

public class CoinFlip {

private enum Coin {
HEADS,
TAILS
};

public static void main(String[] arguments) {

new CoinFlip();
}

CoinFlip() {

Scanner input = new Scanner(System.in);
int heads = 0, tails = 0;

while (true) {
System.out.println("How many flips do you want?");
System.out.println("(0 will exit the program)");
int number = input.nextInt();
if (number == 0)
break; // or System.exit

for (int i = 0; i < number; i++) {
Coin flipResult = flip();
switch (flipResult) {
case HEADS:
heads++;
break;
case TAILS:
tails++;
break;
}
}
System.out.println("Heads: " + heads);
System.out.println("Tails: " + tails);
}
}

private Coin flip() {

return Coin.values()[(int) (Math.random() * Coin.values().length)];
}
}

while 循环继续要求用户一次又一次地玩,如果他们输入 0,则中断它或退出它。在该循环中,for 循环将迭代翻转。

关于java - 将 'menu' 替换为用户的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540698/

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