2016-03-03 57 views
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; 
} 

}

回答

0

你永远不会重新分配H和T移动

h = hareMove(h); 
t = tortoiseMove(t); 

原语的值传递之后,这样其他的改动,你中招了以H和T作出方法不反映在原始值中。

+0

这和我做的一些其他更改一起工作。谢谢! – Jes