[英]Can't figure out how to reset an int so I can use my loop properly making the game of Pig
輪到計算機時,我需要它繼續前進直到當前回合達到35點,但是我不確定如何在新的回合中將其重置為0,因此每次計算機達到35點后,它只會轉一旦。
import java.util.Scanner;
import java.util.Random;
public class PigGamev1
{
public static void main(String[] args)
{
int humanScore = 0;
int computerScore = 0;
int win = 100;
int die1 = 0;
int die2 = 0;
int total = 0;
boolean gameOver = false;
boolean turnOver = false;
char repeat = 'r';
String input;
Scanner keyboard = new Scanner(System.in);
Random randomGenerator = new Random();
do
{
//human rolls dice
do
{
die1 = randomGenerator.nextInt(6) + 1;
die2 = randomGenerator.nextInt(6) + 1;
total = die1 + die2;
if(total == 2)
{
humanScore += 0;
System.out.println("You rolled a: " + total);
System.out.println("Turn over.");
System.out.println("Your current score is " + humanScore);
turnOver = true;
}
else if(humanScore <= win && total != 2)
{
humanScore += total;
System.out.println("You rolled a: " + total);
if(humanScore >= win)
{
System.out.println("Your score is " + humanScore + " and my score is "
+ computerScore);
System.out.println("You win!");
turnOver = true;
gameOver = true;
System.exit(0);
}
System.out.println("Your current score is " + humanScore);
System.out.println("Enter r to roll again or h to hold.");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 'r')
{
turnOver=false;
}
else if(repeat == 'h')
{
System.out.println("Turn over.");
System.out.println("Your current score is " + humanScore +
" total computer score is " + computerScore + "\n");
turnOver = true;
}
}
}while(turnOver == false);
//computer rolls dice
System.out.println("My turn! \n");
do
{
turnOver = false;
die1 = randomGenerator.nextInt(6) + 1;
die2 = randomGenerator.nextInt(6) + 1;
total = die1 + die2;
System.out.println("I rolled a: " + total);
if(total == 2)
{
computerScore += 0;
System.out.println("Turn over.");
System.out.println("My current score is " + computerScore + "\n");
turnOver = true;
}
else
{
computerScore += total;
System.out.println("My current score is " + computerScore + "\n");
if(computerScore >= 35)
{
System.out.println("I hold.");
System.out.println("Your turn!\n");
System.out.println("My current score is " + computerScore +
" your score is " + humanScore + "\n");
turnOver = true;
}
}
if(computerScore >= win)
{
System.out.println("I win!");
turnOver = true;
gameOver = true;
System.exit(0);
}
}while(turnOver == false);
}while(gameOver == false);
keyboard.close();
}
}
使用不同的變量來跟蹤計算機的總得分及其在當前回合中的得分。 例如,您可以定義int computerTurn;
與其余變量一起使用,並在程序中按原樣使用它,切記在回合結束時將其重置為0:
//some code omitted if not relevant to your problem
do
{
if(total == 2)
{
computerScore += computerTurn; //adds current turn to running total
System.out.println("Turn over.");
System.out.println("My current score is " + computerScore + "\n");
turnOver = true;
}
else
{
computerTurn+= total;
System.out.println("My current score is " + computerScore + "\n");
if(computerTurn >= 35) //checks against current turn instead of total
{
computerScore += computerTurn; //adds current turn to running total
System.out.println("I hold.");
System.out.println("Your turn!\n");
System.out.println("My current score is " + computerScore +
" your score is " + humanScore + "\n");
turnOver = true;
}
}
}while(turnOver == false);
computerTurn = 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.