来自扫描仪的Java循环/用户输入

时间:2013-02-06 02:35:31

标签: java

制作一个简单的篮球计划,我要求主队名称,本赛季有多少场比赛,然后循环请求下一场比赛。基本上当我启动do-while循环时,除非用户键入“俄亥俄州立大学”,否则它的效果很好。例如,输出将从“剩余的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 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:3)

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

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

你必须添加

scanInput.nextLine();

这是因为在输入游戏数量后按enter键(行尾)和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提到的变化。

由于

答案 2 :(得分:0)

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

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));
    }
}

答案 3 :(得分:0)

主题:带扫描仪的'Java循环'

我编写的简单Java程序工作正常,你可以自己尝试......你也可以轻松地将这个程序转换为'while循环','do - while循环'和'for - each循环'。< / p>

拉​​菲克, VA,USA, 日期:04/17/2015

//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);          
        }
    }
}