我该如何做一个循环,只显示前4局,然后问他们是否想再玩一次?



我有一个任务,我应该做一个模拟棒球比赛使用链表。到目前为止,我可以打印出所有的打击者,并显示他们在三出局后的一局中有多少分/安打。我遇到的问题是,我至少要打4局,至少得1分,才能要求再次上场。我不能想出一个方法来做一个循环,它将显示所有局与每一局的比分。这是最终输出的样子,这是我的显示方式。正如你所看到的,它一直在问我是否想在每一局后再玩一遍,并且不显示第二局,即使它已经打印出来了。

我的代码
import java.util.Base64;
import java.util.Random;
import java.util.Scanner;
public class baseball {
public static void main(String[] args) {
baseball b = new baseball();
b.addPlayer(1, "Ted Williams", .344, .0687);
b.addPlayer(2, "Rogers Hornsby", .359, .0375);
b.addPlayer(3, "Mark McGwire", .265, .095);
b.addPlayer(4, "Babe Ruth", .340, .085);
boolean keepGoing = true;
while (keepGoing) {
b.startGame();
Scanner s = new Scanner(System.in);
System.out.printf("nPlay again?: Y/N");
if (s.nextLine().equalsIgnoreCase("N")) {
keepGoing = false;
}
}
}
private void startGame() {
int ct = 0;
int inning = 0;
int outs = 0;
int hits = 0;
int score = 0;
int htct = 0;
baseball.Players current = head;
baseball.Players end = tail;
System.out.printf("n ------------- Inning: %s - Game Score: %s Runs", ++inning, score);
while (outs != 3) {
Random r = new Random();
double rr = r.nextDouble();
if (current.bAvg > rr) {
System.out.printf("nnBatter %s - %s ->  BA: %s Random Number: %.2f HIT", ++ct, current.name, current.bAvg, rr);
++htct;
if (htct >= 3) {
hits++;
score = hits;
}
end.next = head;
} else if (current.hrAvg > rr) {
System.out.printf("nnBatter %s - %s ->  BA: %s Random Number: %.2f HOMERUN", ++ct, current.name, current.bAvg, rr);
} else if (current.bAvg < rr) {
System.out.printf("nnBatter %s - %s ->  BA: %s Random Number: %.2f Miss", ++ct, current.name, current.bAvg, rr);
outs++;
if (outs == 3) {
System.out.printf("n--------------- Inning Totals: %s Runs %s Hits", hits, htct);
}
end.next = head;
}
current = current.next;
}
System.out.printf("n ------------ Inning: %s - Game Score: %s Runs", ++inning, score);
}
public class Players {
public Integer bOrder;
public String name;
public Double bAvg;
public Double hrAvg;
Players previous;
Players next;
public Players(int bOrder, String name, Double bAvg, Double hrAvg) {
this.bOrder = bOrder;
this.name = name;
this.bAvg = bAvg;
this.hrAvg = hrAvg;
this.next = null;
this.previous = null;
}
}
Players head, tail = null;
public void addPlayer(int bOrder,String name, Double bAvg, Double hrAvg) {
Players players = new Players(bOrder,name,bAvg,hrAvg);
if (head == null) {
//its the first node
head = players;
tail = players;
head.previous = null;
tail.next = null;
} else {
tail.next = players;
players.previous = tail;
tail = players;
tail.next = null;
}
}
}

我尝试将keepGoing循环添加到我的方法中,但它似乎仍然不起作用,它变成了一个无限循环

添加游戏计数器并在每次游戏开始后增加它。修改while (keepGoing):

int numberOfGames = 0;    \ initialize game counter
while (keepGoing) {
b.startGame();
numberOfGames++; \ increment counter when game is played
if(numberOfGames==3) \ if 3 then offer play again option
{
Scanner s = new Scanner(System.in);
System.out.printf("nPlay again?: Y/N");
if (s.nextLine().equalsIgnoreCase("N")) {
keepGoing = false;
} else 
numberOfGames =0; // reset counter if want to play 
}


}

相关内容

最新更新