Results 1 to 15 of 15
Thread: Please help. Simple question
- 06-15-2011, 11:13 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Please help. Simple question
Alright to I have to make an algorithm and loop it to find all the prime numbers inbetween 0 and whatever, anyways, i have to have the output prime numbers come out in a grid/chart, I have it some what correct but whenever i run it, the higher the number i input, the lower the chart appears untill it is off the the page, as you will see when you compile it. I am using Java Ready to Program. im a noob. Please help me
// The "Cain_Culminating" class.
import java.awt.*;
import hsa.Console;
public class Cain_Culminating
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
int a, b, d, e, f, g, h, i, w, z;
c.println ("\t\t Welcome to my culminating program!");
c.println ("\t\t Please input the upper bound number...");
z = c.readInt ();
a = z + 1;
d = 0;
int gridrow = 8;
int gridcol = 1;
boolean[] array;
array = new boolean [a + 1];
for (f = 1 ; f < a ; f = f + 1)
{
d = 0;
for (b = 1 ; b < f ; b++)
{
e = f % b;
// c.println (e);
if (e == 0)
{
d = d + 1;
}
}
{
if (d > 1)
{
array [f] = false;
}
else if (d <= 1)
{
array [f] = true;
}
}
{
c.clear ();
for (w = 1 ; w < a ; w++)
{
if (array [w] == true)
{
c.setCursor (gridrow, 16 + (gridcol * 5));
c.println (w);
gridcol = gridcol + 1;
}
if (gridcol % 9 == 0)
{
gridrow = gridrow + 1;
gridcol = 1;
}
}
}
}
// Place your program here. 'c' is the output console
} // main method
} // Cain_Culminating class
- 06-15-2011, 11:36 PM #2
Where is the hsa.Console class defined?
What does the setCursor() method do?
- 06-15-2011, 11:47 PM #3
Member
- Join Date
- Jun 2011
- Location
- San Diego, CA
- Posts
- 24
- Rep Power
- 0
It may be do to a c.println(variableName) executing in a loop when the variableName is blank. In that case it would just keep adding a new line. the larger the number entered, the more times the loops iterated, the more c.println()'s are executed adding more new lines. Maybe an IF statement to make sure the variable isn't blank before running. c.println.
- 06-15-2011, 11:56 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
what would i do to make the IF statement?
- 06-16-2011, 12:00 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
is there any other way to display a variable's outuput without using c.print(ln)?
- 06-16-2011, 12:15 AM #6
Most people use: System.out.println(ln);
What does the setCursor() method do?
- 06-16-2011, 12:17 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
it defines the grid layout
- 06-16-2011, 12:17 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I'm not sure the code is correct near the end. Specifically you might be making gridrow bigger when you don't need to, but it's hard to tell with that crazy indentation.
Post the code indented as you see it. Don't use "quote" use the "code" tags instead. Put [CODE] at the start of the code and [/CODE] at the end; that way the indents will be preserved when the code appears here.
- 06-16-2011, 12:18 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Java Code:// The "Cain_Culminating" class. import java.awt.*; import hsa.Console; public class Cain_Culminating { static Console c; // The output console public static void main (String[] args) { c = new Console (); int a, b, d, e, f, g, h, i, w, z; c.println ("\t\t Welcome to my culminating program!"); c.println ("\t\t Please input the upper bound number..."); z = c.readInt (); a = z + 1; d = 0; int gridrow = 8; int gridcol = 1; boolean[] array; array = new boolean [a + 1]; for (f = 1 ; f < a ; f = f + 1) { d = 0; for (b = 1 ; b < f ; b++) { e = f % b; // c.println (e); if (e == 0) { d = d + 1; } } { if (d > 1) { array [f] = false; } else if (d <= 1) { array [f] = true; } } { c.clear (); for (w = 1 ; w < a ; w++) { if (array [w] == true) { c.setCursor (gridrow, 16 + (gridcol * 5)); c.println (w); gridcol = gridcol + 1; } if (gridcol % 9 == 0) { gridrow = gridrow + 1; gridcol = 1; } } } } // Place your program here. 'c' is the output console } // main method } // Cain_Culminating class
- 06-16-2011, 12:19 AM #10
Is the grid layout related to the problem?
- 06-16-2011, 12:20 AM #11
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
ya , put it in your compiler and you will see that the grid doesnt display correctly
- 06-16-2011, 12:34 AM #12
What is the "it" you mention? I don't have your IDE or the Console class so I can NOT see what happens.put it in your compiler
Is the setCursor() method necessary for your program?
Can you remove it and execute the program and see what happens.
If the setCursor() method is causing your problem, what is it doing wrong? What values are you giving it that are wrong?
Print out the values that you give the setCursor() method to see what the values are.
For example:
System.out.println("c.setCursor (gridrow, 16 + (gridcol * 5) gr=" + gridrow + " gc=" + gridcol );
- 06-16-2011, 12:36 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Perhaps you should get rid of the braces except for those used with for and if/else. It would make the code easier to follow. (As would using descriptive variable names)
Why do you nest the two for loops? (put one inside the other) It means that the output is forever clearing the display and redrawing itself which seems like a waste of time. Another approach would be for the first for loop to populate array (give its elements values). And for the output code - from c.clear() onwards - to occur after that first loops has finished.
The second for loop - that one that does the output - should do nothing at all if array[w] is not true. At the moment you have it increasing gridrow too often: gridrow should only increase just after gridcol has been changed.
[Edit] Many folk don't have this hsa pacakge and so can't run your program. And many of the rest wouldn't anyway: they will rely on the code itself and your description of the problem being intelligible.Last edited by pbrockway2; 06-16-2011 at 12:38 AM.
- 06-16-2011, 12:40 AM #14
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
could you edit the pieces of code you are talking about and display them here? sorry its a little confusing
- 06-16-2011, 01:07 AM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
Simple question
By Qsc in forum New To JavaReplies: 6Last Post: 03-06-2011, 11:24 PM -
Simple Question
By stackptr89 in forum New To JavaReplies: 13Last Post: 01-29-2011, 05:35 PM -
very simple Question
By arsenal4ever_11 in forum NetBeansReplies: 2Last Post: 05-27-2010, 08:51 PM -
some simple question?
By jperson in forum New To JavaReplies: 4Last Post: 05-03-2010, 05:32 PM -
Probably a really simple question...
By ibanez270dx in forum New To JavaReplies: 0Last Post: 11-16-2007, 01:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks