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

Thread: whats wrong?

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default whats wrong?

    public class Programme {

    there is no error for this code:

    public static void main(String[] args) {

    int count = 0;

    for (int i = 10; i < 50; i++) {
    if (isprime(i)) {
    count++;
    System.out.println(i + "is a prime number. ");

    if (count == 3) {
    break;
    }
    }
    }

    }

    public static boolean isprime(int n) {

    if (n == 1) {
    return false;
    }

    for (int i = 2; i <= n / 2; i++)
    {
    if (n % i == 0)
    {
    return false;
    }

    }
    return true;

    }
    }

    however when i put the "return true;" into an else statement like this i get an error. Why is that?
    public class Programme {

    public static void main(String[] args) {

    int count = 0;

    for (int i = 10; i < 50; i++) {
    if (isprime(i)) {
    count++;
    System.out.println(i + "is a prime number. ");

    if (count == 3) {
    break;
    }
    }
    }

    }

    public static boolean isprime(int n) {

    if (n == 1) {
    return false;
    }

    for (int i = 2; i <= n / 2; i++)
    {
    if (n % i == 0)
    {
    return false;
    }
    else
    {
    return true;
    }
    }


    }
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: whats wrong?

    Please specific your problem
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: whats wrong?

    when i put
    return true after the for loop
    my program works
    for (int i = 2; i <= n / 2; i++)
    {
    if (n % i == 0)
    {
    return false;
    }

    }
    return true;

    }
    }


    but when i put return true into an else statement my program doesn't work like this

    for (int i = 2; i <= n / 2; i++)
    {
    if (n % i == 0)
    {
    return false;
    }
    else
    {
    return true;
    }
    }

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: whats wrong?

    You're missing return statement
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: whats wrong?

    what do you mean by missing? I put my return statement into an else statement

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: whats wrong?

    You need to add return statement after else brackets
    Whatever you are, be a good one

  7. #7
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: whats wrong?

    is that a rule for java?

Similar Threads

  1. whats wrong
    By Jdino23 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2014, 04:42 PM
  2. I don't even know whats wrong.
    By Gugubo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 22nd, 2013, 08:20 PM
  3. Whats wrong here??
    By cbplayer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2013, 02:19 PM
  4. Whats going wrong here?
    By choll4c in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2012, 03:39 PM
  5. WHATS WRONG
    By subodhkr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 8th, 2012, 07:47 AM