私のクラスでは、私はサイコロのゲームを作る必要があります、私は戦争のバージョンを作った、私は少し助けを完了する必要があります。私は最後のポイントを表示する必要があり、何とか私はスコアの違いを表示することができたら欲しいです。私はまた、ループオーバーでゲームを終了する方法を知らない。私はゲームを終了するか、または続行する必要があります。ループwhileクラスのためのループ
public class Game {
public static void main(String[] args)
{
Dice myDie = new Dice();
Dice CompDie= new Dice();
int player =0;
int computer =0;
boolean gameOver;
do
{
int[] scores = new int[3];
gameOver = false;
while(!gameOver)
{
myDie.roll();
System.out.println("You rolled a " + myDie.getValue());
player = myDie.getValue();
CompDie.roll();
System.out.println("Computer rolled a " + CompDie.getValue());
computer = CompDie.getValue();
checkResults(player, computer, scores);
printResults(scores);
// ask player if want to continue enter Y to continue
System.out.println("Do you want to continue playing enter Y if so"); // I need to end loop here
}
}while(keepPlaying());
}
public static boolean keepPlaying()
{
Scanner readIn = new Scanner(System.in);
boolean playAgain = false;
System.out.println("Do you want to play again?");
String answer = readIn.nextLine().toUpperCase();
char ans = answer.charAt(0);
if(ans == 'Y')
playAgain = true;
return playAgain;
}
public static void checkResults(int player, int computer, int[] scores)
{
if(player > computer)
{
scores[1]++;
}
else if(player < computer)
{
scores[2]++;
}
else
{
scores[0]++;
}
}
public static void printResults(int[] list)
{
System.out.println(" Ties Player Computer");
for (int i = 0; i < list.length; i++)
{
System.out.printf("%8d", list[i]);
}
System.out.println();
}
}
最も内側のループは完全に不要です。それを取り除き、その内部のコードを最初のループの中に入れるだけです。また、最初のループの前に 'scores'を初期化する必要があります。そうでなければ、ループが実行されるたびにブランク配列として再作成され、以前に追加された値は保持されません。 –
この質問はIDEとは関係がないので、これを 'eclipse'とタグ付けする必要はありません。 –
私はループを終了しましたが、あなたが言ったように初期化する必要があります。どうすればいいですか? – Lucas