Question:

Help with my JAVA program

by  |  earlier

0 LIKES UnLike

Hi,

I am supose to create a program that asks the user to input a number, and it will print out every prime less than or equal to the entered number. I have a few errors everytime I try to compile it.

will someone please help me figure out what my errors are? I have been working on this for awhile now and i finally got it down to a few syntax errors but I just can't figure out what they are.

here's what i've got so far:

import javax.swing.*;

public class Problem1 {

public static void main( String[] args ) {

int max = Integer.parseInt( JOptionPane.showInputDialog(

"Largest number that you want to test for primality." ) );

System.out.print( "Primes <= " max " are:" );

computePrimes( max );

}

public static void computePrimes( int m ) {

for ( int i = 1; i =< m; i ) {

if ( isPrime( i ) ) {

System.out.print( i ", " );

}

}

}

public static boolean isPrime( int ) {

if ( i == 1 ) {

return true;

}

for ( int j = 2; j <= i/2; j ) {

if ( ( i mod j ) == 0 ) {

return false;

}

}

return true;

}

}

 Tags:

   Report

1 ANSWERS


  1. 1) Your system.out.print needs to have the various values concatenated with +, ie &quot;Primes &lt;= &quot; + max + &quot; are: &quot;

       Same thing for the other system.out.print

    2) Your for loops are not quite right.  The last value operand should be i++ and j++ respectively.  Otherwise, the counters will never been incremented.

    May be something with the Swing code.  I don&#039;t Swing that way, so I wouldn&#039;t know.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.