5

制作一个简单的篮球程序,我询问主队名称,本赛季有多少场比赛,然后循环询问下一场球队比赛。基本上,当我启动 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);
4

4 回答 4

4

Replace: sAwayTeam = scanInput.next();withsAwayTeam = scanInput.nextLine();它循环两次的原因是因为scanInput.next();一次只返回一个标记(例如单词)。当您输入两个单词时,它不需要在第二次继续之前接收来自用户的更多输入,因为它已经有另一个单词要返回。因此双循环。

您还需要注意调用nextInt(). 这与next()方法类似,但它不是一个标记(单词),而是只扫描一个字符作为 int。试试这个:iNumGames = scanInput.nextInt();put scanInput.nextLine();This 应该清除 scanInput 任何使它跳过的东西。注意:由于您的代码的编写方式,这只会读取一个字符。如果您需要读取多个字符,您应该使用nextLine()并将其值分配给一个整数。

于 2013-02-06T02:51:44.577 回答
3

Donny Schrimsher 给出的答案中所说的都是正确的。你现在要做的就是在获得主队篮球赛季的比赛场数之后,即

System.out.println("How many games are in the home team's basketball season?");
iNumGames = scanInput.nextInt();

你必须添加

scanInput.nextLine();

这是因为在输入游戏数后,您按下回车键(行尾),nextInt() 方法采用游戏数而不是行尾。此行尾由 Donny Schrimsher 在 do-while 循环中提到的 nextLine() 方法使用。所以为了避免这种情况,你添加了一个额外的 nextLine() 方法。

因此它必须是

System.out.println("How many games are in the home team's basketball season?");
iNumGames = scanInput.nextInt();
scanInput.nextLine();
System.out.println(iNumGames);

加上 Donny Schrimsher 提到的变化。

谢谢

于 2013-02-06T04:04:45.610 回答
1

尝试使用所有退出功能的以下代码。

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: ");
            scanInput = new Scanner(System.in);
            sAwayTeam = scanInput.nextLine();
            if(!"".equals(sAwayTeam.trim()) && !"exit".equals(sAwayTeam.trim()))
            {

                System.out.println(sAwayTeam);
                iGamesLeft = --iNumGames;
                System.out.println("There are " + iGamesLeft+ " games left in the basketball season");

            }

        }// end do
        while (iGamesLeft > 0 && !"exit".equalsIgnoreCase(sAwayTeam));
    }
}
于 2013-02-06T04:20:30.293 回答
0

主题:带有扫描仪的“Java 循环”

我写的这个简单的Java程序运行良好,你可以自己试试……你也可以很容易地把这个程序转换成'while循环','do - while循环'和'for - each循环'。

美国弗吉尼亚州拉菲克,日期:2015 年 4 月 17 日

//Examples: 'for loop' with Scanner
package com.java_basics;

import java.util.Scanner;

public class ForLoop_Examples_With_Scanner 
{
    public static void main(String[] args) 
    {
        //Creating instance of Scanner to allows a user's input to read from System.in
        Scanner mySC = new Scanner(System.in);
        System.out.println("Please, enter the value of 'int i' between '0 and 2' : ");
        int i = mySC.nextInt();

        System.out.println("Please, enter the value of 'exitPoint' between '10 and 1000' :");
        int exitPoint = mySC.nextInt();

        System.out.println("Please, enter the value of 'increment' between '1 and 2' :");
        int increment = mySC.nextInt();

        mySC.close();//Releasing memory to the OS (Operating System) for reuse

        System.out.println("Output:\n" + "======");
        for(;i<exitPoint ; i=i+increment)//i++==>i=i+1==>i=i+increment
        {
            System.out.println(i);          
        }
    }
}
于 2015-04-17T17:55:42.870 回答