[英]How to track and move String from left to right?
我正在使用 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 |
您不會在任何地方存儲賽車手的位置。 從您的代碼中,每次調用*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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.