私はシンプルなコインフリッププログラムを書こうとしています。私はかなりJavaに慣れていて、ユーザーに何回何回コインを振りたいのかを尋ねようとしていました。ここに私のコードは次のとおりです。Java Coin Flip Program
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のように感じますトラックが、イム立ち往生は...任意の助けをいただければ幸いです。..
EDIT:学習の興味にあるので
私は私のプログラムを変更し、列挙型にコインを変更し、プログラムのリターンを持っていることにしましたそのenum value..iはメニューのスタイルへのユーザー入力を変更しましたが、これはBarnes and Noblesの本をよく読んでいます。私は基本的に2つのプログラムを一緒に融合して、enum値などを返す限りすべての新しい仕事がまだそこに残っていたが、 'メニュー'のアスペクトを削除して置き換えたいと思っていた前のプログラムから何回フリップをしたいのかをユーザが入力する能力。
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(choice != 0 && numTosses !=0)
のようにwhileループチェックを配合して、数を減少させ、私はその数を確認する必要があり、それがどのように多くのヘッドとどのように多くの尾限り0印刷結果に到達すると、ユーザーに促します彼らがゲームをやり直したいのであれば、私は知りません。なぜなら、私は知識の面でこれをやろうとしているのですから、もしあなたがブロスキを助けるのをやめたいなら、私は分かります。
2つの接続されていないセグメントではなく、完全なコードを送信できますか? –
'int number = input.nextInt();'の後に 'input.nextLine();'を追加してみてください。 –
指示を表示する場所に 'print'の代わりに' println'を使用するとどうなりますか? –