This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-12-14 19:45:22

matthew8092001
Scratcher
Registered: 2010-09-12
Posts: 100+

Prime number help- Java

Yeah, so I was trying to make the computer list 100 prime numbers. But when I execute this, it only shows one: 3. (No, I do not want 2.)
Could someone please help try to make this print 100 prime numbers instead of just one? I'm not sure if it has something to do with the break statement, but it shouldn't cancel all loops, just the one that it is encased in.
Thanks in advance!

Code:

public class primenumbers {
    public static void main (String[]args){
    int prime = 0;
    int repeater = 1;
    int number = 3;
    for (repeater = 1; repeater < 100; repeater++)/*repeat 100 times*/{
    
    int divisor = 2;
    for(divisor = 2; divisor < number; divisor++ ){
        if (number % divisor == 0){
            prime = 0;
            break;
        }
        else{
            prime = 1;
        }
    }
    if (prime==1){
    System.out.println(number);
    number++;
    }
    
    }/*end for loop here*/
    
    }}

http://1.bp.blogspot.com/_GVA115I1I8Y/TMGwbJ056kI/AAAAAAAABHs/kpvg0oCKV2g/s1600/happy.gif
lol

Offline

 

#2 2011-12-14 20:12:32

GameHutSoftware
Scratcher
Registered: 2010-04-24
Posts: 1000+

Re: Prime number help- Java

this goes in advanced topics
yeah that makes no sense
but a couple of my flash threads got moved there


Lurking more than posting, but still here.

Offline

 

Board footer