Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Problem with running random class

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with running random class

    My program complies and runs, but the random method is selecting randoms numbers instead of the numbers I assigned the colours? Any hints on hoe I can fix this??


    import java.util.*;
    import java.util.Random;
     
    public class AssigOne112
    {
    	public static void main(String[] args)
    	{
    		// Initialising Scanner and setting Random
    		Scanner sc= new Scanner(System.in);
    		Random generator;
     
    		// Set seed value
    		final int SEED = 400;
     
    		// Colour variable for vehicles
    		final int BLACK = 0;
    		final int BLUE = 1;
    		final int CYAN = 2;
    		final int DARK_GREY = 3;
    		final int GREY = 4;
    		final int GREEN = 5;
    		final int LIGHT_GREY = 6;
    		final int MAGENTA = 7;
    		final int ORANGE = 8;
    		final int PINK = 9;
    		final int RED = 10;
    		final int WHITE = 11;
    		final int YELLOW = 12;
     
    		// Value of coloured cars
    		final int BLACK_CAR_VALUE = 1;
    		final int WHITE_CAR_VALUE = 1;
    		final int GREEN_CAR_VALUE = 2;
    		final int YELLOW_CAR_VALUE = 3;
    		final int BLUE_CAR_VALUE = 4;
    		final int ORANGE_CAR_VALUE = 5;
    		final int CYAN_CAR_VALUE = 0;
    		final int DARK_GREY_CAR_VALUE = 0;
    		final int GREY_CAR_VALUE = 0;
    		final int LIGHT_GREY_CAR_VALUE = 0;
    		final int MAGENTA_CAR_VALUE = 0;
    		final int PINK_CAR_VALUE = 0;
     
     
     
    		//Calculated players average
    		double avgScore1;
    		double avgScore2;
     
     
    		// Variables
    		// Players name input
    		String name1;
    		String name2;
    		String playerOne;
    		String playerTwo;
    		String name;
     
    		// Players sequence
    		boolean playerSequence;
    		int currentPlayer;
     
    		// Variables for players scoring
    		int player1Total = 0;
    		int player2Total = 0;
    		int scoreInning = 0;
     
    		// Final score
    		int score =0;
     
    		// Assigns winner
    		int won = 0;
     
    		// Assigns number of innings
    		int playGame = 0;
     
    		// Starts count for innings
    		int innings = 0;
    		int colourNum = 0;
    		String colour;
     
    		// Numbers 1-9 are cars, number 0 is other vehicle
    		int vehicleType = 0;
    		boolean isCar = (vehicleType !=0);
     
    		//Game Rules
    		System.out.println("Car Cricket Game");
    		System.out.println("================");
    		System.out.println("Scoring: non white/black/red/green/yellow/blue/orange car: 0 runs.");
    		System.out.println("White/black car: 1 run.");
    		System.out.println("Non-red van/truck/bus/SUV: 1 run.");
    		System.out.println("Green car: 2 runs.");
    		System.out.println("Yellow car: 3 runs.");
    		System.out.println("Blue car: 4 runs.");
    		System.out.println("Orange car: 5 runs.");
    		System.out.println("Red van/truck/bus/SUV: 6 runs.");
    		System.out.println("Red car: OUT!");
    		System.out.println();
    		System.out.println("Time to play!");
    		System.out.println();
     
     
    		//Ask user to input names and innings wanting to play.
    		System.out.print("Two players are required to play this game.  Please enter the name of one of the players: ");
    		playerOne = sc.next();
     
    		System.out.print("Please enter the name of the other player: ");
    		playerTwo = sc.next();
     
    		System.out.print("Answering either true or false, is " + playerOne + " going to bat first? ");
    		playerSequence = sc.nextBoolean();
    		System.out.print("How many innings do you want to play in this game? ");
    		playGame = sc.nextInt();
     
    		//installation of Random function
    		generator = new Random(SEED);
     
    		// For loop to count innings
    		for(innings=1; innings<=playGame; innings ++)
    		{
    			System.out.println("Innings # " + innings);
     
    			// For loop to alternate between players
    			for(currentPlayer = 1; currentPlayer <=2; currentPlayer ++)
    			{
    				if(playerSequence)
    				{
    					name1 = playerOne;
    					name2 = playerTwo;
    				}
    				else
    				{
    					name1 = playerTwo;
    					name2 = playerOne;
    				}
    				System.out.print("Player #" + currentPlayer + " ");
     
    				// determine current player's name
    				if (currentPlayer == 1)
    				{
    					name = name1;
    				}
    				else
    				{
    					name = name2;
    				}
    				System.out.println("(" + name + ")");
     
    				// add score to a global scoring of given player
    				if (currentPlayer == 1)
    				{
    					player1Total = player1Total + score;
    				}
    				else
    				{
    					player2Total = player2Total + score;
    				}
     
    				// While loop continues until red colur is choosen
    				while(colourNum!=RED)
    				{
    					colourNum = generator.nextInt(13);
    					vehicleType = generator.nextInt(10);
     
    					score = colourNum;
    					colour = "";
     
    					if (vehicleType >= 1 && vehicleType <= 9)
    					{
    						isCar = true;
     
    						// add score by given colour to inning's score
    						scoreInning += score;
     
    						switch (colourNum)
    						{
    							case BLACK:
    										colour = "Black";
    										score = score + BLACK_CAR_VALUE;
     
    										break;
    							case BLUE:
    										colour = "Blue";
    										score = score + BLUE_CAR_VALUE;
    										break;
    							case CYAN:
    										colour = "Cyan";
    										score = score + CYAN_CAR_VALUE;
    										break;
    							case DARK_GREY:
    										colour = "Dark Grey";
    										score = score + DARK_GREY_CAR_VALUE;
    										break;
    							case GREY:
    										colour = "Grey";
    										score = score + GREY_CAR_VALUE;
    										break;
    							case GREEN:
    										colour = "Green";
    										score = score + GREEN_CAR_VALUE;
    										break;
    							case LIGHT_GREY:
    										colour = "Light Grey";
    										score = score + LIGHT_GREY_CAR_VALUE;
    										break;
    							case MAGENTA:
    										colour = "Magenta";
    										score = score + MAGENTA_CAR_VALUE;
    										break;
    							case ORANGE:
    										colour = "Orange";
    										score = score + ORANGE_CAR_VALUE;
    										break;
    							case PINK:
    										colour = "Pink";
    										score = score + PINK_CAR_VALUE;
    										break;
    							case RED:
    										colour = "Red";
    										//score = score + RED_CAR_VALUE;
    										break;
    							case WHITE:
    										colour = "White";
    										score = score + WHITE_CAR_VALUE;
    										break;
    							case YELLOW:
    										colour = "Yellow";
    										score = score + YELLOW_CAR_VALUE;
    										break;
    						}
     
    						System.out.println(colour + " car..." + score);
    					}
     
     
    					if(vehicleType==0)
    					{
    						isCar = false;
    						System.out.println(colour + "  van/truck/bus/SUV..." + score);
    					}
    				}
     
    				// If loop for random red colour selection
    				if(colourNum == RED)
    				{
    					isCar=true;
    					System.out.println("OUT!");
    					System.out.println("Total was " + scoreInning);
    					System.out.println("Current scores: ");
    					System.out.println(name1 + ": " + player1Total);
    					System.out.println(name2 + ": " + player2Total);
    				}
     
    				if(isCar = false)
    				{
    					colourNum = RED;
    					scoreInning += 6;
    					if (currentPlayer == 1)
    					{
    						player1Total = sc.nextInt();
    						player1Total += 6;
    					}
    					else
    					{
    						player2Total = sc.nextInt();
    						player2Total += 6;
    					}
    					System.out.println("6");
    				}
    				else
    				{
    					scoreInning += 1;
    					if (currentPlayer == 1)
    					{
    						player1Total += 1;
    					}
    					else
    					{
    						player2Total += 1;
    					}
    					System.out.println("1");
    				}
     
    				//Find the winner.
    				if (player1Total == player2Total)
    				{
    					System.out.println("Draw!");
    				}
    				else
    				if (player1Total > player2Total)
    				{
    					avgScore1 =  player1Total/playGame;
    					System.out.println(name1 + " (with avg " + avgScore1 + ") won");
    				}
    				else
    				if (player1Total < player2Total)
    				{
    					avgScore2 =  player2Total/playGame;
    					System.out.println(name2 + " (with avg " + avgScore2 + ") won");
    				}
    			}
    		}
    	}
    }
    Last edited by Gizken; April 26th, 2012 at 09:56 PM. Reason: reading


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    This is a strange looking while statement:
    while(currentPlayer = 1 && currentPlayer <=2 && currentPlayer ++)
    Does this code compile for you?
    What do you want it to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    Hi Norm, it is a game played by two players that state how many innings they want to play. It then should alternate between the two players, within each innings, random cars and non cars of different colours pass until the random selects a red car, which means the player is out. And then it is the next players turn.

    When I run the program, it states first player name and innings 1 and then does nothing, I dont know why it does start randomly selecting the colours and vehicles??? I hope you can help me

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    The code you posted does not compile. How can you execute the program with compiler errors?

    Please post the full text of the compiler errors you are getting.

    The code needs to be formatted. It should not all start in the first column.
    There should be indentations for each nesting level of the logic within loops and if statements. Unformatted code is very hard to read and understand.
    Last edited by Norm; April 17th, 2012 at 06:55 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    Here are the compiler errors:
    --------------------Configuration: <Default>--------------------
    :305: while expected
    }
    ^
    :307: illegal start of expression
    }
    ^
    :307: ')' expected
    }
    ^
    :308: reached end of file while parsing
    }
    ^
    :309: reached end of file while parsing
    
    ^
    5 errors

    Process completed.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    :305: while expected
    Is there a do without a while()?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    I have a while loop inside the do loop, is this wrong?
    Last edited by Gizken; April 17th, 2012 at 09:02 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    No it is ok to have loops inside of loops. No problem there.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    I have rewritten my program it compiles, but it is not entering into the while loop??? any suggestions???

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    Post the new code that compiles.

    Try debugging it by adding printlns that print out the values of the variables used in the while statement so you can see what the computer sees and know why it is doing what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    Take away the do and its brackets, and take away the semicolon from the end of the while loop.

    Should be this
    import java.util.*;
    import java.util.Random;
     
    public class AssigOne112
    {
    	public static void main(String[] args)
    	{
    		Scanner sc= new Scanner(System.in);
    		Random generator;
     
    		//Set seed value
    		final int SEED = 400;
     
    		//Final var
    		final int CARS = 4;
     
    		//Colour variable for vehicles
    		final int BLACK = 0;
    		final int BLUE = 1;
    		final int CYAN = 2;
    		final int DARK_GREY = 3;
    		final int GREY = 4;
    		final int GREEN = 5;
    		final int LIGHT_GREY = 6;
    		final int MAGENTA = 7;
    		final int ORANGE = 8;
    		final int PINK = 9;
    		final int RED = 10;
    		final int WHITE = 11;
    		final int YELLOW = 12;
     
    		//Value of coloured cars
    		final int BLACK_CAR_VALUE = 1;
    		final int WHITE_CAR_VALUE = 1;
    		final int GREEN_CAR_VALUE = 2;
    		final int YELLOW_CAR_VALUE = 3;
    		final int BLUE_CAR_VALUE = 4;
    		final int ORANGE_CAR_VALUE = 5;
    		final int CYAN_CAR_VALUE = 0;
    		final int DARK_GREY_CAR_VALUE = 0;
    		final int GREY_CAR_VALUE = 0;
    		final int LIGHT_GREY_CAR_VALUE = 0;
    		final int MAGENTA_CAR_VALUE = 0;
    		final int PINK_CAR_VALUE = 0;
     
     
     
    		//Calculated players average
    		double avgScore1;
    		double avgScore2;
     
     
    		//Variables
    		// players name input
    		String name1;
    		String name2;
    		String playerOne;
    		String playerTwo;
    		String name;
     
    		//Players sequence
    		boolean playerSequence;
    		int currentPlayer;
    		int player;
     
    		//Variables for players scoring
    		int player1Total;
    		int player2Total;
    		int scoreInning = 0;
     
    		//Final score
    		int score =0;
     
    		int won = 0;
     
    		int playGame = 0;
     
    		String getColour;
    		int innings = 0;  //starts count for innings
    		int colourNum = -1;
    		String colour;
    		int getScoreByColour;
     
    		int vehicleType = -1;
     
    		boolean isCar = (vehicleType !=0);
     
    		//Game Rules
    		System.out.println("Car Cricket Game");
    		System.out.println("================");
    		System.out.println("Scoring: non white/black/red/green/yellow/blue/orange car: 0 runs.");
    		System.out.println("White/black car: 1 run.");
    		System.out.println("Non-red van/truck/bus/SUV: 1 run.");
    		System.out.println("Green car: 2 runs.");
    		System.out.println("Yellow car: 3 runs.");
    		System.out.println("Blue car: 4 runs.");
    		System.out.println("Orange car: 5 runs.");
    		System.out.println("Red van/truck/bus/SUV: 6 runs.");
    		System.out.println("Red car: OUT!");
    		System.out.println();
    		System.out.println("Time to play!");
    		System.out.println();
     
    			//Ask user to input names and innings wanting to play.
    			System.out.print("Two players are required to play this game.  Please enter the name of one of the players: ");
    			playerOne = sc.next();
     
    			System.out.print("Please enter the name of the other player: ");
    			playerTwo = sc.next();
     
    			System.out.print("Answering either true or false, is " + playerOne + " going to bat first? ");
    			playerSequence = sc.nextBoolean();
    			System.out.print("How many innings do you want to play in this game? ");
    			playGame = sc.nextInt();
     
    			//installation of Random function
    			generator = new Random(SEED);
     
    		while(innings<=playGame)
    		{
    			for(innings=1; innings<=playGame; innings ++)
    			{
    				System.out.println("Innings # " + innings);
     
    				for(currentPlayer = 1; currentPlayer <=2; currentPlayer ++)
    				{
    					if(playerSequence)
    					{
    						name1 = playerOne;
    						name2 = playerTwo;
    					}
    					else
    					{
    						name1 = playerTwo;
    						name2 = playerOne;
    					}
    					System.out.print("Player #" + currentPlayer + " ");
     
    					// determine current player's name
    					if (currentPlayer == 1)
    					{
    						name = name1;
    					}
    					else
    					{
    						name = name2;
    					}
    					System.out.println("(" + name + ")");
    					System.out.println();
    					player1Total = sc.nextInt();
    					player2Total = sc.nextInt();
     
    					// add score to a global scoring of given player
    					if (currentPlayer == 1)
    					{
    						player1Total +=score;
    					}
    					else
    					{
    						player2Total +=score;
    					}
     
    					if (vehicleType != 0)
    					{
    						isCar = true;
    						colourNum = generator.nextInt(13);
    						score = colourNum;
    						vehicleType = generator.nextInt(10) + 1;
    						//return vehicleType >= 1 && vehicleType <= 9;
     
    						// add score by given colour to inning's score
    						scoreInning += score;
     
    						System.out.print(" car...");
     
    						if(colourNum == RED)
    						{
    							System.out.println("OUT!");
    							System.out.println("Total was " + scoreInning);
    							System.out.println("Current scores: ");
    							System.out.println(name1 + ": " + player1Total);
    							System.out.println(name2 + ": " + player2Total);
    						}
     
    						colour = "";
    						switch (colourNum)
    						{
    						case BLACK:
    							colour = "Black";
    							score = BLACK_CAR_VALUE;
     
    							break;
    						case BLUE:
    							colour = "Blue";
    							score = BLUE_CAR_VALUE;
    							break;
    						case CYAN:
    							colour = "Cyan";
    							score = CYAN_CAR_VALUE;
    							break;
    						case DARK_GREY:
    							colour = "Dark Grey";
    							score = DARK_GREY_CAR_VALUE;
    							break;
    						case GREY:
    							colour = "Grey";
    							score = GREY_CAR_VALUE;
    							break;
    						case GREEN:
    							colour = "Green";
    							score = GREEN_CAR_VALUE;
    							break;
    						case LIGHT_GREY:
    							colour = "Light Grey";
    							score = LIGHT_GREY_CAR_VALUE;
    							break;
    						case MAGENTA:
    							colour = "Magenta";
    							score = MAGENTA_CAR_VALUE;
    							break;
    						case ORANGE:
    							colour = "Orange";
    							score = ORANGE_CAR_VALUE;
    							break;
    						case PINK:
    							colour = "Pink";
    							score = PINK_CAR_VALUE;
    							break;
    						case RED:
    							colour = "Red";
    							//score = score + RED_CAR_VALUE;
    							break;
    						case WHITE:
    							colour = "White";
    							score = WHITE_CAR_VALUE;
    							break;
    						case YELLOW:
    							colour = "Yellow";
    							score = YELLOW_CAR_VALUE;
    							break;
    						}
    						return;
    					}
     
    					else
    					{
    						if (colourNum == RED)
    						{
    							isCar = false;
    							System.out.print("  van/truck/bus/SUV...");
    							scoreInning += 6;
    							if (currentPlayer == 1)
    							{
    								player1Total += 6;
    							}
    							else
    							{
    								player2Total += 6;
    							}
    							System.out.println("6");
    						}
    						else
    						{
    							scoreInning += 1;
    							if (currentPlayer == 1)
    							{
    								player1Total += 1;
    							}
    							else
    							{
    								player2Total += 1;
    							}
    							System.out.println("1");
    						}
    					}
    					//Find the winner.
    					if (player1Total == player2Total)
    					{
    						System.out.println("Draw!");
    					}
    					else if (player1Total > player2Total)
    					{
    						avgScore1 =  player1Total/playGame;
    						System.out.println(name1 + " (with avg " + avgScore1 + ") won");
    					}
    					else if (player1Total < player2Total)
    					{
    						avgScore2 =  player2Total/playGame;
    						System.out.println(name2 + " (with avg " + avgScore2 + ") won");
    					}
    				}
    			}
    		}
    	}
    }

  12. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    I have edited code in first post with changes I have made. I wil try the debugging you have suggested, thanks

  13. #13
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    & try splitting it up, using a settings class -- will make the entire code a lot cleaner. Also, your doing a lot of weird stuff that you don't really need to in the code.

  14. #14
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    you have a little bit of a misunderstanding on how do while loops work.
    Instead, take away the do and the semicolon at the end of the while
    Theres
    do{
    //code
    }while(this statement is still true);

    and then theres
    while(this statement is true){
    //code
    }

  15. #15
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    I have done debugging and worked out whats going wrong:

    player1Total = sc.nextInt();

    This is where my program stops, but when I remove it, I get error message : variable player1Total might not have been initialized

    I'm not sure how to initialse it as I thought
    player1Total += score;
    was correct initialisation?

  16. #16
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    Have you first corrected the error I've pointed out?

    What I'm getting

    Car Cricket Game
    ================
    Scoring: non white/black/red/green/yellow/blue/orange car: 0 runs.
    White/black car: 1 run.
    Non-red van/truck/bus/SUV: 1 run.
    Green car: 2 runs.
    Yellow car: 3 runs.
    Blue car: 4 runs.
    Orange car: 5 runs.
    Red van/truck/bus/SUV: 6 runs.
    Red car: OUT!

    Time to play!

    Two players are required to play this game. Please enter the name of one of the players: a
    Please enter the name of the other player: b
    Answering either true or false, is a going to bat first? true
    How many innings do you want to play in this game? 1
    Innings # 1
    Player #1 (a)

    1
    2
    car...

    & then it terminates.
    Last edited by CjStaal; April 17th, 2012 at 09:55 PM.

  17. #17
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    You need to take the return out of your switch statment next -- it's ending the game when it gets to that.

    Now I'm getting
    Car Cricket Game
    ================
    Scoring: non white/black/red/green/yellow/blue/orange car: 0 runs.
    White/black car: 1 run.
    Non-red van/truck/bus/SUV: 1 run.
    Green car: 2 runs.
    Yellow car: 3 runs.
    Blue car: 4 runs.
    Orange car: 5 runs.
    Red van/truck/bus/SUV: 6 runs.
    Red car: OUT!

    Time to play!

    Two players are required to play this game. Please enter the name of one of the players: a
    Please enter the name of the other player: b
    Answering either true or false, is a going to bat first? true
    How many innings do you want to play in this game? 2
    Innings # 1
    Player #1 (a)

    1
    1
    car...Draw!
    Player #2 (b)

    1
    1
    car...Draw!
    Innings # 2
    Player #1 (a)

    1
    1
    car...Draw!
    Player #2 (b)

    1

    1
    car...b (with avg 1.0) won

    import java.util.*;
    import java.util.Random;
     
    public class AssigOne112
    {
    	public static void main(String[] args)
    	{
    		Scanner sc= new Scanner(System.in);
    		Random generator;
     
    		//Set seed value
    		final int SEED = 400;
     
    		//Final var
    		final int CARS = 4;
     
    		//Colour variable for vehicles
    		final int BLACK = 0;
    		final int BLUE = 1;
    		final int CYAN = 2;
    		final int DARK_GREY = 3;
    		final int GREY = 4;
    		final int GREEN = 5;
    		final int LIGHT_GREY = 6;
    		final int MAGENTA = 7;
    		final int ORANGE = 8;
    		final int PINK = 9;
    		final int RED = 10;
    		final int WHITE = 11;
    		final int YELLOW = 12;
     
    		//Value of coloured cars
    		final int BLACK_CAR_VALUE = 1;
    		final int WHITE_CAR_VALUE = 1;
    		final int GREEN_CAR_VALUE = 2;
    		final int YELLOW_CAR_VALUE = 3;
    		final int BLUE_CAR_VALUE = 4;
    		final int ORANGE_CAR_VALUE = 5;
    		final int CYAN_CAR_VALUE = 0;
    		final int DARK_GREY_CAR_VALUE = 0;
    		final int GREY_CAR_VALUE = 0;
    		final int LIGHT_GREY_CAR_VALUE = 0;
    		final int MAGENTA_CAR_VALUE = 0;
    		final int PINK_CAR_VALUE = 0;
     
     
     
    		//Calculated players average
    		double avgScore1;
    		double avgScore2;
     
     
    		//Variables
    		// players name input
    		String name1;
    		String name2;
    		String playerOne;
    		String playerTwo;
    		String name;
     
    		//Players sequence
    		boolean playerSequence;
    		int currentPlayer;
    		int player;
     
    		//Variables for players scoring
    		int player1Total;
    		int player2Total;
    		int scoreInning = 0;
     
    		//Final score
    		int score =0;
     
    		int won = 0;
     
    		int playGame = 0;
     
    		String getColour;
    		int innings = 0;  //starts count for innings
    		int colourNum = -1;
    		String colour;
    		int getScoreByColour;
     
    		int vehicleType = -1;
     
    		boolean isCar = (vehicleType !=0);
     
    		//Game Rules
    		System.out.println("Car Cricket Game");
    		System.out.println("================");
    		System.out.println("Scoring: non white/black/red/green/yellow/blue/orange car: 0 runs.");
    		System.out.println("White/black car: 1 run.");
    		System.out.println("Non-red van/truck/bus/SUV: 1 run.");
    		System.out.println("Green car: 2 runs.");
    		System.out.println("Yellow car: 3 runs.");
    		System.out.println("Blue car: 4 runs.");
    		System.out.println("Orange car: 5 runs.");
    		System.out.println("Red van/truck/bus/SUV: 6 runs.");
    		System.out.println("Red car: OUT!");
    		System.out.println();
    		System.out.println("Time to play!");
    		System.out.println();
     
    		//Ask user to input names and innings wanting to play.
    		System.out.print("Two players are required to play this game.  Please enter the name of one of the players: ");
    		playerOne = sc.next();
     
    		System.out.print("Please enter the name of the other player: ");
    		playerTwo = sc.next();
     
    		System.out.print("Answering either true or false, is " + playerOne + " going to bat first? ");
    		playerSequence = sc.nextBoolean();
    		System.out.print("How many innings do you want to play in this game? ");
    		playGame = sc.nextInt();
     
    		//installation of Random function
    		generator = new Random(SEED);
     
    		while(innings<=playGame)
    		{
    			for(innings=1; innings<=playGame; innings ++)
    			{
    				System.out.println("Innings # " + innings);
     
    				for(currentPlayer = 1; currentPlayer <=2; currentPlayer ++)
    				{
    					if(playerSequence)
    					{
    						name1 = playerOne;
    						name2 = playerTwo;
    					}
    					else
    					{
    						name1 = playerTwo;
    						name2 = playerOne;
    					}
    					System.out.print("Player #" + currentPlayer + " ");
     
    					// determine current player's name
    					if (currentPlayer == 1)
    					{
    						name = name1;
    					}
    					else
    					{
    						name = name2;
    					}
    					System.out.println("(" + name + ")");
    					System.out.println();
    					player1Total = sc.nextInt();
    					player2Total = sc.nextInt();
     
    					// add score to a global scoring of given player
    					if (currentPlayer == 1)
    					{
    						player1Total +=score;
    					}
    					else
    					{
    						player2Total +=score;
    					}
     
    					if (vehicleType != 0)
    					{
    						isCar = true;
    						colourNum = generator.nextInt(13);
    						score = colourNum;
    						vehicleType = generator.nextInt(10) + 1;
    						//return vehicleType >= 1 && vehicleType <= 9;
     
    						// add score by given colour to inning's score
    						scoreInning += score;
     
    						System.out.print(" car...");
     
    						if(colourNum == RED)
    						{
    							System.out.println("OUT!");
    							System.out.println("Total was " + scoreInning);
    							System.out.println("Current scores: ");
    							System.out.println(name1 + ": " + player1Total);
    							System.out.println(name2 + ": " + player2Total);
    						}
     
    						colour = "";
    						switch (colourNum)
    						{
    						case BLACK:
    							colour = "Black";
    							score = BLACK_CAR_VALUE;
     
    							break;
    						case BLUE:
    							colour = "Blue";
    							score = BLUE_CAR_VALUE;
    							break;
    						case CYAN:
    							colour = "Cyan";
    							score = CYAN_CAR_VALUE;
    							break;
    						case DARK_GREY:
    							colour = "Dark Grey";
    							score = DARK_GREY_CAR_VALUE;
    							break;
    						case GREY:
    							colour = "Grey";
    							score = GREY_CAR_VALUE;
    							break;
    						case GREEN:
    							colour = "Green";
    							score = GREEN_CAR_VALUE;
    							break;
    						case LIGHT_GREY:
    							colour = "Light Grey";
    							score = LIGHT_GREY_CAR_VALUE;
    							break;
    						case MAGENTA:
    							colour = "Magenta";
    							score = MAGENTA_CAR_VALUE;
    							break;
    						case ORANGE:
    							colour = "Orange";
    							score = ORANGE_CAR_VALUE;
    							break;
    						case PINK:
    							colour = "Pink";
    							score = PINK_CAR_VALUE;
    							break;
    						case RED:
    							colour = "Red";
    							//score = score + RED_CAR_VALUE;
    							break;
    						case WHITE:
    							colour = "White";
    							score = WHITE_CAR_VALUE;
    							break;
    						case YELLOW:
    							colour = "Yellow";
    							score = YELLOW_CAR_VALUE;
    							break;
    						}
    					}
     
    					else
    					{
    						if (colourNum == RED)
    						{
    							isCar = false;
    							System.out.print("  van/truck/bus/SUV...");
    							scoreInning += 6;
    							if (currentPlayer == 1)
    							{
    								player1Total += 6;
    							}
    							else
    							{
    								player2Total += 6;
    							}
    							System.out.println("6");
    						}
    						else
    						{
    							scoreInning += 1;
    							if (currentPlayer == 1)
    							{
    								player1Total += 1;
    							}
    							else
    							{
    								player2Total += 1;
    							}
    							System.out.println("1");
    						}
    					}
    					//Find the winner.
    					if (player1Total == player2Total)
    					{
    						System.out.println("Draw!");
    					}
    					else if (player1Total > player2Total)
    					{
    						avgScore1 =  player1Total/playGame;
    						System.out.println(name1 + " (with avg " + avgScore1 + ") won");
    					}
    					else if (player1Total < player2Total)
    					{
    						avgScore2 =  player2Total/playGame;
    						System.out.println(name2 + " (with avg " + avgScore2 + ") won");
    					}
    				}
    			}
    		}
    	}
    }

    You have a lot of unused variables, and I don't think it's running how you want. I suggest looking back in your textbook and re-reading the basics. They're fundamental to every program.
    Last edited by CjStaal; April 17th, 2012 at 10:01 PM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    This is where my program stops
    The Scanner method is waiting for you to enter a number.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with running random class

    I have edited my original program but still having problems with the random method, it is choosoing its own numbers instead of the numbers I assigned to tthe colours, any hints on how I can fix this?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with running random class

    it is choosoing its own numbers instead of the numbers I assigned to tthe colours
    Please explain and show what the code is doing. Add some printlns to print out what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Random Errors Occurring When Running Game in Eclipse
    By WhenThCome4Me in forum Java IDEs
    Replies: 22
    Last Post: September 1st, 2011, 06:29 AM
  3. Help with running class file
    By carface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 30th, 2010, 05:40 PM
  4. running a class from another projects, reflection
    By led1433 in forum Object Oriented Programming
    Replies: 7
    Last Post: July 29th, 2009, 01:47 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM