我想写一个简单的硬币翻转程序,并想知道我是否可以得到一些帮助。我对Java相当陌生,只是试图询问用户他们想抛硬币多少次。这里是我的代码:Java硬币翻转程序
package cointossing;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
/**
* Coin tossing class to simulate the flip of a coin
* with two sides; Heads and Tails.
*
* @author Alex Chapman ID:
*
*/
public class CoinTossing
{
public static String sideUp;
public static int number;
public void run()
{
try(Scanner input = new Scanner(in))
{
out.print("Enter how many times you would like to flip the coin");
out.print("if you enter 0 the program quits");
int number = input.nextInt();
}
}
private static void coin()
{
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0)
{
sideUp = "heads";
}
else
{
sideUp = "tails";
}
}
public static String getsideup()
{
out.println(sideUp);
return sideUp;
}
public static void main(String[] args)
{
int hcount = 0;
int tcount = 0;
for (int i = 1; i <= number; i++)
{
coin();
if (getsideup().equals("heads"))
{
hcount++;
}
else
{
tcount++;
}
}
out.println("total heads = " + hcount + " total tails = " + tcount);
}
}
但是当我运行程序它跳过询问用户任何东西,只是显示为0的,因为没有次数翻转硬币...我觉得右边IM轨道,但即时通讯卡...任何帮助将不胜感激..
编辑:
所以在学习的兴趣,我决定改变我的计划,改变硬币枚举并有计划返回该枚举值..我也改变了用户输入菜单样式,但这是通过跟随在一个巴恩斯和贵族书,我购买了一段时间后帮助...我瘦ki来到了一个奇怪的十字路口......我想要基本上将两个程序融合在一起,这样所有新的工作,只要返回枚举值,并且仍然存在,但删除“菜单”方面并将其替换为用户可以输入他们想要从前一个程序中做多少翻转的能力。这里是我写的新程序:
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);
}
}
我正在考虑创建一个新变量并让用户设置掷骰子数。然后加入化合物while循环检查,像这样
while(choice != 0 && numTosses !=0)
,然后减少计数,我得检查计数,一旦它尽可能达到0打印结果多少头和多少的尾巴,然后提示用户如果他们想再次玩这个游戏..我真的不知道为什么我试图做到这一点,但是对于知识方面,所以如果你不想帮我理解一个混蛋。
您可以发布完整的代码而不是两个断开连接的段? –
尝试在'int number = input.nextInt();'行后面添加'input.nextLine();'。 –
如果在显示说明的地方使用'println'而不是'print',会发生什么情况? –