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: Can anyone help me to find what code im wrong? thx

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can anyone help me to find what code im wrong? thx

    /* Program: Random number generator
    * Written by: Chaitanya from beginnersbook.com
    * Input: None
    * Output:Random number between o and 200*/
    import java.util.*;
    class GenerateRandomNumber {
    public static void main(String[] args) {
    int counter;
    Random rnum = new Random();
    int jackpot = rnum();
    /* Below code would generate 5 random numbers
    * between 0 and 200.
    */
    System.out.println("Random Numbers:");
    System.out.println("***************");
    for (counter = 1; counter <= 5; counter++) {
    System.out.println(rnum.nextInt(200));
    if( jackpot = 100){
    /* This println statement will only execute,
    * if the above condition is true
    */
    System.out.println("jackpot!");
    }
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can anyone help me to find what code im wrong? thx

    find what code im wrong
    Please explain why you think there is something wrong with the code.
    If there are error messages, copy the full text and paste it here.
    If the output is wrong, copy the full text and paste it here with comments saying what is wrong.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can anyone help me to find what code im wrong? thx

    two problems i can see, rnum is an instance of class Random, it is not a method that returns an int that you can assign to int variable jackpot. Also in your loop when you generate each random number you need to save it to a variable before you can print and test it.
    int jackpot;
     
    for (counter = 1; counter <= 5; counter++) {
        jackpot = rnum.nextInt(200);
        System.out.println(jackpot);
        /* This println statement will only execute,
         * if the above condition is true
         */
        if( jackpot = 100){
            System.out.println("jackpot!");
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can anyone help me to find what code im wrong? thx

    @tonya - Did you compile and execute the posted code? Did it work?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help me to find what code im wrong? thx

    it worked thx! I just had to change ur line if( jackpot = 100){

    to

    if( jackpot == 100){

Similar Threads

  1. Replies: 4
    Last Post: February 21st, 2021, 11:03 AM
  2. Replies: 3
    Last Post: April 30th, 2014, 08:03 AM
  3. I cannot find the problem with this code.
    By LiveH in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 11th, 2013, 10:53 PM
  4. Can't Find what is wrong with my Code
    By Raptorman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 04:12 PM
  5. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM