while loop issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: while loop issue

  1. #1
    Join Date
    Aug 2010
    Posts
    1

    while loop issue

    I am taking an intro java summer class and this is one of the assignments. I was going along pretty smoothly with my code until I wrote this for loop and tried to test my progress.

    The loop takes in input to create the size of the array of objects. It is for a tournament bracket matchup, so it needs to be 4, 8, 16, 32, or 64. Problem is that when I use || it no longer works.
    If I use a statement like while (teamCount != 4) it works fine.

    Code:
    /* Tournament.java
    This program takes user input of teams and number of wins to create
    an array of team objects and makes pairings of teams for a tournament
    @author Gary C. Rogers 2010
    */
    
    import java.util.Scanner;
    
    public class Tournament
    {
    	public static void main(String[] args)
    	{
    		int teamCount;
    		int teamWins;
    		String name;
    		
    		Scanner input = new Scanner(System.in);
    		//Ask the user for thr number of teams for the tournament
    		System.out.println("What is the number of teams in the tournament?");
    		teamCount = input.nextInt();
    		
    		//Make sure that the input is valid for matchups
    		while (teamCount!=4 || teamCount !=8 || teamCount !=16 || teamCount !=32 || teamCount !=64)
    		{
    			System.out.println("You have entered an invalid number of teams.  Please try again.");
    			
    			System.out.println("What is the number of teams in the tournament?");
    			teamCount = input.nextInt();
    		}
    
    		//Declares the array to store objects
    		Team[] teamArray = new Team[teamCount];
    		int count = 0;
    			
    		//Loop allows for the input for the team objects
    		do
    		{
    			System.out.println("What is the team name?");
    			name = input.next();
    				
    			System.out.println("How many wins do they have?");
    			teamWins = input.nextInt();
    				
    			teamArray[count]= new Team( name, teamWins);
    			count++;
    				
    			if (count < teamCount)
    			{
    				System.out.println("Team information gathered.  Input next team.");
    			}
    				
    			else
    			{
    				System.out.println("All teams have been inputed.  Thank you.");
    			}
    		}
    			
    		while (count < teamCount);
    			
    		for (int index = 0; index < teamArray.length; index++)
    		{
    				
    			System.out.println(teamArray[index].getTeamName() + " " + teamArray[index].getWins());
    				
    		}
    				
    		
    		
    	}
    }
    There still needs to be a whole structure written to make the matchups, but for now this displays the team objects that the user inputs.

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,422

    Re: while loop issue

    If the user entered 8 would teamCount != 4 be true?

    If you want all of the conditions to be true to have the code enter the while loop, use the AND operator. With OR, only one of the tests must be true to enter the while.
    Norm

  3. #3
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: while loop issue

    You could also consider using the modulus operator for your while condition. It returns the remainder of dividing its first argument by the second. For example:

    Code:
    System.out.println(5 &#37; 2);
    will output one. If you always want a multiple of four for your team count, you could use it like this:

    Code:
    while ((teamCount % 4) != 0)
    Then you don't have to worry about someone putting in 128 and your code failing that input, or dealing with the multiple similar conditions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center