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 2 of 2

Thread: Probmen with randon class in program

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

    Question Probmen with randon class in program

    Hi I am writing a program for a car cricket game. I can get the innings and players name ok, but when the program needs to randomly choose vehicles and colours, nothing appears in output. I think my program is not entering in this loop. Could I please get some suggestions? My code so far:

    [hightlight=java]

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

    for(innings=1; innings<=playGame; innings ++)
    {
    System.out.println("Innings # " + innings);

    do
    {
    while(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();


    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;

    // add score to a global scoring of given player
    if (currentPlayer == 1)
    {
    player1Total +=score;
    }
    else
    {
    player2Total +=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");
    }
    }

    //player1Total = sc.nextInt();
    //player2Total = sc.nextInt();


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

    }
    }
    }
    }
    }
    [/highlight]


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

    Default Re: Probmen with randon class in program

    Answered at http://www.javaprogrammingforums.com...dom-class.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Two class program help
    By BuhRock in forum Object Oriented Programming
    Replies: 4
    Last Post: July 4th, 2012, 05:27 PM
  2. Need major help with a program for class
    By burnenator in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 20th, 2011, 10:55 PM
  3. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  4. Class design for GUI program
    By albertkao in forum Object Oriented Programming
    Replies: 1
    Last Post: January 6th, 2011, 12:05 PM
  5. new program (lotto.class)
    By james137 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2009, 05:22 PM