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

Thread: for loop with random numbers

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post for loop with random numbers

    Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3, inclusive. The numbers represent the type of vehicle as stated below:

    Number Vehicle Category
    1 Car
    2 Motor bikes
    3 Trucks

    Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program. How do i do it so they will add up the sum of each vehicle? Thank you.

    The answer should be something like
    Number of cars = X
    Number of motor bikes = Y
    Number of Trucks = Z

    but i'm getting
    Total number of vehicle:
    cars
    motorbikes
    motorbikes
    cars
    Trucks
    Trucks
    motorbikes
    motorbikes
    Trucks
    cars

    public static void main(String[] args) {
    int countCar = 0;
    int countBike = 0;
    int countTruck = 0;
    int all=0;



    for (int series = 1; series<=10; series++) {


    all = (int) (Math.random()*3)+1;


    if (all==1)
    {
    System.out.println(" cars " );
    }
    else if (all==2)
    {
    System.out.println(" motorbikes " );
    }
    else if (all==3)
    {
    System.out.println(" Trucks " );
    }
    }
    }
    }


  2. #2
    Junior Member codepoet's Avatar
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop with random numbers

    You should change the content of your conditions to countXxx++

    ex. if your vehicle code for truck is 3 then add 1 to countTruck

    then at the end of the for loop, thats where you print the counters

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop with random numbers

    Sorry but i don't understand. Is there anyone who're willing to write a similar java code so i can learn better. Basically the for loop is used because the question asked me to generate a series of 10 random intergers = for (int series = 1; series<=10; series++) {
    then there will generate a number between 1 and 3 with all = (int) (Math.random()*3)+1; or i have to do them individually? like countCar = (int) (Math.random()*3)+1; etc etc and that's all i can do.

    1. how do i summaries the 10 generated code example instead of cars motorbikes motorbikes cars Trucks Trucks motorbikes motorbikes Trucks cars, i get 3 cars, 3 trucks and 4 bikes?

    so the answer will be
    Number of cars = 3
    Number of motor bikes = 4
    Number of Trucks = 4

    sorry if i'm asking alot.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: for loop with random numbers

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

    Write out the steps your program needs to accomplish and code them one at a time, testing each step as you go. I recommend you start by generating 10 random numbers with values 1 through 3, inclusive. Once you have that, you can increment a counter for each type of vehicle based on the value of each random number generated. There are a few steps after that, but you can work them out.

    Come back when you've made some progress and need help.

  5. #5
    Junior Member codepoet's Avatar
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop with random numbers

    *** Code removed
    Last edited by Norm; July 6th, 2014 at 07:36 PM. Reason: Please don't do OPs work for him

Similar Threads

  1. Do while loop to generate 6 random numbers
    By Bran.NET in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 5th, 2014, 12:45 AM
  2. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM
  3. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  4. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM