制作一个简单的篮球程序,我询问主队名称,本赛季有多少场比赛,然后循环询问下一场球队比赛。基本上,当我启动 do-while 循环时,它工作得很好,除非用户输入例如“Ohio State”。例如,输出将从“剩余 6 场比赛”变为“剩余 4 场比赛”。通常它只会问对手?,然后递减一局。
我该如何解决,以使 2 个单词的篮球队名称不会减少两次?
import java.util.Scanner;
public class Basketball2 {
public static void main(String[] args) throws java.io.IOException {
Scanner scanInput = new Scanner(System.in);
String sHomeTeam;
String sAwayTeam;
int iNumGames;
int iGamesLeft = 0;
System.out.println("Enter home team's name: ");
sHomeTeam = scanInput.nextLine();
System.out.println(sHomeTeam);
System.out.println("How many games are in the home team's basketball season?");
iNumGames = scanInput.nextInt();
System.out.println(iNumGames);
//start looping
do {
System.out.println("Enter opponent team's name: ");
sAwayTeam = scanInput.next();
System.out.println(sAwayTeam);
iGamesLeft = --iNumGames;
System.out.println("There are " + iGamesLeft + " games left in the basketball season");
}//end do
while(iGamesLeft > 0);