2013-02-06 70 views
4

製作一個簡單的籃球程序,我要求主隊名稱,本賽季有多少場比賽,然後在一個循環中請求下一場比賽。基本上,當我開始do-while循環時,除非用戶輸入例如「Ohio State」,否則它效果很好。例如,輸出將從「剩餘6場比賽」到「剩餘4場比賽」。通常它會問對手?然後減少一場比賽。來自掃描儀的Java循環/用戶輸入

我該如何解決這樣一個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); 
+3

使用'nextLine()',而不是'下一個()'掃描對手隊名時。 – Vulcan

回答

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

加上由唐尼Schrimsher提到的變化。

謝謝

4

替換:sAwayTeam = scanInput.next();sAwayTeam = scanInput.nextLine();它循環兩次是因爲scanInput.next();每次只返回一個令牌(例如字)的原因。當您輸入兩個單詞時,由於它已經有另一個單詞要返回,因此在繼續第二次之前不需要接收來自用戶的更多輸入。因此,雙循環。

編輯:你還需要照顧的代碼行,呼籲nextInt()。這與next()方法一樣工作,但是,它不是一個標記(單詞),而是僅以一個字符作爲int掃描。試試這個:在iNumGames = scanInput.nextInt();之後放scanInput.nextLine();這應該清除scanInput中任何讓它跳過的東西。注意:由於您的代碼被寫入的方式,這隻會讀取一個字符。如果您需要閱讀多個字符,則應使用nextLine()並將其值分配給一個整數。

編輯:沒有看到@ Rameshwar.S.Soni發表了什麼。這是正確的。

+0

當我這樣做時,它立即返回「籃球賽季還剩下_場比賽。」沒有我輸入球隊名稱。 –

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

主題:「Java的循環」與掃描儀

我寫的是可以正常使用的簡單的Java程序,你可以自己嘗試...你也可以將此程序很容易地轉換成「while循環」 ,'do - while loop'和'for - each loop'。

拉菲克, VA,USA, 日期爲: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);   
     } 
    } 
}