0
我已經看到了關於烏龜和兔子比賽的幾個問題,但它們都屬於小程序,我不是編程一個小程序。該程序是爲了我的程序的顯示輸出有一些麻煩。我想有該程序顯示輸出,看起來是這樣的:爲什麼我的Java Tortoise和Hare Race無法正常顯示?
______T__________H________________________________
__________T_______________________H_______________
_____________T________H___________________________
等,直到烏龜或野兔到達終點並贏得了比賽。截至目前,我的輸出如下所示:
______________________________HT__________________________________________________HT__________________________________________________HT_
在似乎是一個無限循環。這裏是我的代碼:
public class TortoiseAndHair {
public static void main (String [] args)
{
int t = 0; // Keeps track of tortoise progress
int h = 0; // Keeps track of hare progress
System.out.println("AND THEY'RE OFF!!");
while (t < 50 || h < 50)
{
hareMove(h);
tortoiseMove(t);
if (t>1 && h> 1 && t == h) // Display when tortoise and hare occupy same space beyond start
{
System.out.print("OUCH!!");
}
if (t < 1)
{ // Prevents tortoise from slipping behind start
t = 1;
}
if (h < 1)
{ // Prevents hare from slipping behind start
h = 1;
}
if (t > 50)
{ // Prevents tortoise from going passed finish line
t = 50;
}
if (h > 50)
{ // Prevents hare from going passed finish line
h = 50;
}
for (int count = 1; count <= 50; count++)
{
System.out.print("_");
if (count == h)
{
System.out.print("H");
}
if (count == t)
{
System.out.print("T");
}
}
if (h < 50 && t == 50)
{ //Output if tortoise wins
System.out.print("TORTOISE WINS!!");
}
if (t < 50 && h == 50)
{ // Output if hare wins
System.out.print("HARE WINS!!");
}
if (h == 50 && t == 50)
{
System.out.print("IT'S A TIE!!");
}
}
}
/**
* This method will calculate the random integer that will dictate the tortoise's movements on the board,
* use that random integer to determine the tortoise's movements (tMove), and add that to the counter keeping track
* of the tortoise's position
* @param t is an int variable that is keeping track of the tortoises position on the board
* @return the tortoise's current position on the board after that turn
* Pre-Conditions: n is an int between 1 and 10, t is a positive int greater than 0 and less than 50
*/
public static int tortoiseMove (int t)
{
int n;
int tMove = 0;
n = (int) (10 * Math.random()) + 1; // Generates random number between 1 and 10
if (n > 10)
{ // ensures n doesn't go higher than 10
n = 10;
}
// Series of if/else statements to control tMove
if (n >= 1 && n <= 5)
{ // Fast plod if n is between 1 and 5
tMove = 3;
}
else
{
if (n >= 6 && n <= 8)
{ // Slow Plod if n is between 6 and 8
tMove = 1;
}
else
{
if (n == 9 || n == 10)
{ // Slip if n is 9 or 10
tMove = -6;
}
}
}
// Add determined movement to tortoise counter and return that value
t += tMove;
return t;
}
/**
* This method will calculate the random integer that will dictate the hare's movements on the board,
* use that random integer to determine the hare's movements (hMove), and add that to the counter keeping track
* of the hare's position on the board
* @param h is an int variable keeping track of the hare's current position on the board
* @return the hare's position on the board after current turn
* Pre-Condition: n is an int between 1 and 10, h is a positive int greater than 0 and less than 50
*/
public static int hareMove (int h)
{
int n;
int hMove = 0;
n = (int) (10 * Math.random()) + 1; // Generates random number between 1 and 10
if (n > 10)
{ // ensures n doesn't go higher than 10
n = 10;
}
//Series of if/else statements to control hMove
if (n == 1 || n == 2)
{ // Big hop if n is 1 or 2
hMove = 9;
}
else
{
if (n >= 3 && n <= 5)
{ // Small hop is n is between 3 and 5
hMove = 1;
}
else
{
if (n == 6)
{ // Big slip is n is 6
hMove = -12;
}
else
{
if (n == 7 || n == 8)
{ // Small slip if n is 7 or 8
hMove = -2;
}
else
{
if (n == 9 || n == 10)
{ // Hare falls asleep if n is 9 or 10
hMove = 0;
}
}
}
}
}
// Add determined movement to hare counter and return value of h
h += hMove;
return h;
}
}
這和我做的一些其他更改一起工作。謝謝! – Jes