我正在使用Strings制作赛车游戏,但无法使它们向右移动。我将如何移动琴弦? 方法JamesMove()和KeithMove()与SusanMove()相同,只是变量名称不同。
public class NameRacer {
public static void nameRaces() {
Scanner scan = new Scanner(System.in);
String response;
int SusanFront, SusanBack, JamesFront, JamesBack, KeithFront, KeithBacck;
boolean raceIsDone;
int move = (int)((Math.random() * 5) + 1);
introduction();
scan.nextLine();
do {
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
System.out.println("Would you like to watch the race again?");
response=scan.next();
}
while(response.equalsIgnoreCase("yes"));
}
private static void introduction() {
System.out.println("This game races three names. The random number generator");
System.out.println("from the Math class repeatedly gives each name a random number");
System.out.println("between 1 and 5. As the names move these random distances");
System.out.println("we see which name gets to the finish line first.");
System.out.println("Press a key to begin the race.");
}
private static void SusanMove() {
String SusanFrontString="";
String SusanBackString="";
int SusanFront=50,SusanBack=0;
int move = (int)((Math.random() * 5) + 1);
SusanFront=SusanFront-move;
SusanBack=SusanBack+move;
for(int back = 0; back<SusanBack;back++) {
SusanBackString = SusanBackString+" ";
}
for(int front = 0; front<SusanFront;front++) {
SusanFrontString = SusanFrontString+" ";
}
String SusanRace = SusanBackString+"Susan"+SusanFrontString+"|";
System.out.println(SusanRace);
}
我期望这样的输出:
Susan |
James |
Keith |
Susan |
James |
Keith |
Susan |
James |
Keith |
但是我得到了:
Susan |
James |
Keith |
Susan |
James |
Keith |
Susan |
James |
Keith |
Susan |
James |
Keith |
答案 0 :(得分:1)
您不在任何地方存储赛车手的位置。从您的代码中,对*Move
方法的每次调用就像您的赛车每次都重新启动一样。您的*Front
和*Back
的值始终在每次调用时重新初始化为50和0。
要解决此问题,只需将默认值存储在NameRaces
类中,然后在*Move
方法中删除这些变量的初始化,如下所示:
public class NameRacer {
private static int SusanFront = 50, JamesFront = 50, KeithFront = 50;
private static int SusanBack = 0, JamesBack = 0, KeithBack = 0;
public static void nameRaces() {
Scanner scan = new Scanner(System.in);
String response;
boolean raceIsDone;
int move = (int)((Math.random() * 5) + 1);
introduction();
scan.nextLine();
do {
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
System.out.println("Would you like to watch the race again?");
response=scan.next();
}
while(response.equalsIgnoreCase("yes"));
}
private static void introduction() {
System.out.println("This game races three names. The random number generator");
System.out.println("from the Math class repeatedly gives each name a random number");
System.out.println("between 1 and 5. As the names move these random distances");
System.out.println("we see which name gets to the finish line first.");
System.out.println("Press a key to begin the race.");
}
private static void SusanMove() {
String SusanFrontString="";
String SusanBackString="";
// Removed re-initialization of Front and Back values
int move = (int)((Math.random() * 5) + 1);
SusanFront=SusanFront-move;
SusanBack=SusanBack+move;
for(int back = 0; back<SusanBack;back++) {
SusanBackString = SusanBackString+" ";
}
for(int front = 0; front<SusanFront;front++) {
SusanFrontString = SusanFrontString+" ";
}
String SusanRace = SusanBackString+"Susan"+SusanFrontString+"|";
System.out.println(SusanRace);
}