Results 1 to 13 of 13
Thread: Wrong Output (Java Program)
- 11-22-2010, 06:29 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
Wrong Output (Java Program)
hi guys... this is my program and i dont know what is wrong:S
The problem is in the main i guess..Can you guys help me please? thanks for everything
public class Xiu {
public static void main (String []args)
{
int max = 50;
System.out.print(enigma(max)+ " ");
}
public static boolean[] enigma(int max) {
boolean[] res = new boolean[max+1];
for (int i = 0; i < 2; i++) res[i] = false;
for (int i = 2; i < res.length; i++) res[i] = true;
for (int i = 2; i < res.length; i++)
if (res[i])
for (int j = i; i * j < res.length; j++)
res[i * j] = false;
return res;
}
}
- 11-22-2010, 06:33 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
What are you expecting it to do? What is it doing differently to that?
Also, it would help if you used the [code] tags to format your code.
- 11-22-2010, 06:41 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
i dont know to use code tags for now :S What i want to do is put the method enigma working :S It should give false in 0 and 1 index and then false in i x i index (ex: 2 x 2, 3 x 3, ...) until max + 1. The rest index are true.. I know the method is right, the problem is how to put it working in the main :S
- 11-22-2010, 08:13 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 12
You have to iterate through every element and print it out. What you're probably getting as output is something like this:
@245ae567b76
That's the memory location of the array.Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-27-2010, 01:20 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
yes moonchile im getting that output...i put a for () but still not working
Java Code:public class Xiu { public static void main (String []args) { int max = 50; for (int i = 0; i < max+1; i++) System.out.print(enigma(max)+ " "); } public static boolean[] enigma(int max) { boolean[] res = new boolean[max+1]; for (int i = 0; i < 2; i++) res[i] = false; for (int i = 2; i < res.length; i++) res[i] = true; for (int i = 2; i < res.length; i++) if (res[i]) for (int j = i; i * j < res.length; j++) res[i * j] = false; return res; } }
Last edited by Fubarable; 11-27-2010 at 02:07 PM. Reason: Moderator Edit: code tags added
-
Many of us here will wait for you to learn. To use them, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
-
I've added tags to your post above, but the code in your post wasn't formatted to begin with, and tags won't magically make unformatted code formatted. Also, you need to tell us more, since "it still doesn't work" doesn't tell us much about how it's currently not working.
- 11-27-2010, 02:16 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
ok guys i got it to work.. Thanks for everything.
The progrmam should be like this:
public class Xiu {
public static void main (String []args)
{
int max = 50;
for (int i = 0;i< max+1; ++i)
{
System.out.println ((enigma(max)[i]));
}
}
public static boolean[] enigma(int max)
{
boolean[] res = new boolean[max+1];
for (int i = 0; i < 2; i++) res[i] = false;
for (int i = 2; i < res.length; i++) res[i] = true;
for (int i = 2; i < res.length; i++)
if (res[i])
for (int j = i; i * j < res.length; j++)
res[i * j] = false;
return res;
}
}
OutPut: Prime Numbers (True) until max;
- 11-27-2010, 02:21 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 12
- 11-27-2010, 02:24 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 11-28-2010, 02:31 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
- 11-28-2010, 02:46 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 11-28-2010, 04:28 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 01:37 AM -
Redirecting output of java program to file
By dewitrydan in forum New To JavaReplies: 4Last Post: 11-17-2010, 06:58 AM -
Whats Wrong I m nt Getting output -please please help
By divakantdinesh in forum New To JavaReplies: 3Last Post: 10-16-2010, 09:21 PM -
the explanation of output of simple java program
By amol84 in forum New To JavaReplies: 1Last Post: 11-06-2008, 05:06 PM -
what is wrong with this program ?
By Poor Bee in forum New To JavaReplies: 1Last Post: 05-07-2008, 07:23 PM
Bookmarks