我正在使用字符串制作赛车游戏,但无法让它们向右移动。我该如何移动琴弦? 方法 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 |
最佳答案
您没有在任何地方存储赛车手的位置。从您的代码中,每次调用 *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);
}
关于java - 如何从左到右跟踪和移动字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823118/