|
-
August 1st, 2010, 02:02 PM
#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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|