| View previous topic :: View next topic |
| Author |
Message |
lexie Advanced Cheater
Reputation: 0
Joined: 23 Jan 2007 Posts: 80
|
Posted: Fri Jan 04, 2008 11:38 pm Post subject: code problem (scripts included~change) |
|
|
I have not done any java coding for quite some time and im taking a java class next semester and we have a project due the first day im glad i started now because im having problems with my menu 2 im not sure what im doing wrong but i want it to print the primes in 1 to 100.
please tell me what is wrong.
| Code: |
import TerminalIO.KeyboardReader;
public class primedriver
{
public static void main(String [] args)
{
KeyboardReader reader = new KeyboardReader();
int n=1;
int menu;
boolean prime;
menu=reader.readInt("Press one to find out if a number is prime"+
" or press two to generate a list of prime numbers. ");
if (menu=='1');
{
n=reader.readInt("Enter the number that your unsure about, This will tell you if it is prime. ");
for(int i = 2; i * i <= n; i++)
if(n % i == 0)
{
System.out.print(n+" is a prime number.");
}
else
{
System.out.print(n+" is a composite number.");
}
}
if (menu=='2');
{
do{
for(int i = 2; i * i <= n; i++)
if(n % i == 0)
{
return n;
}
}
while(n<=100);
}
}
}
|
Thanks/im sorry in advance for anytime or effort you spent reading this and helping me
~lexie
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Jan 05, 2008 7:22 am Post subject: Re: code problem (scripts included~change) |
|
|
You do this:
'menu' is not a char variable (or even a string--I don't remember if Java does that for strings). You shouldn't include semicolons (;) at the end of 'if' statements.
Rather, try this:
You'll also need to fix that for your other one.
~~~
Then you have this code:
| Code: |
if (menu=='2'); //take out the 's, and ;
{
do{
for(int i = 2; i * i <= n; i++)
if(n % i == 0)
{
return n; //return is only for use in methods, see below
}
}
while(n<=100);
}
|
If you 'return'ed 'n', then the program would end. You could set up a method, though.
| Code: |
private static bool IsPrime(int i)
{
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0) return true;
}
return false; //This is because if it was a prime number, we'd have
//already 'returned'
} |
Then, in your loop back in the other code:
| Code: |
if (menu == 2)
{
do {
n++; //increment n by one, so we aren't looping forever
if (IsPrime(n)) System.out.print(n);
} while (n <= 100);
}
|
And hell, you could even change your first if clause
| Code: |
if (menu == 1)
{
n = readInt("blah blah"); //I didn't feel like typing that out
if (IsPrime(n)) System.out.print(n+" is a prime number.");
else System.out.print(n+" is a composite number.");
}
|
_________________
|
|
| Back to top |
|
 |
lexie Advanced Cheater
Reputation: 0
Joined: 23 Jan 2007 Posts: 80
|
Posted: Mon Jan 07, 2008 12:21 am Post subject: |
|
|
Wooh
thanks im going to make those changes, i like your class and driver.
~lexie
Edit
New code i have not yet changed it to a class and driver i will soon....
| Code: | import TerminalIO.KeyboardReader;
public class primedriver2
{
public static void main(String [] args)
{
KeyboardReader reader = new KeyboardReader();
int n=0;
int menu;
boolean prime;
menu=reader.readInt("Press one to find out if a number is prime"+
" or press two to generate a list of prime numbers. ");
if (menu==1)
{
n=reader.readInt("Enter the number that your unsure about, This will tell you if it is prime. ");
if (n%2!=0 || n==2)
{
System.out.println(n+" is prime. ");
}
else //if(n%2!=0)
{
System.out.println(n+" is composite. ");
}
}
if (menu==2)
{
n=1;
do{
if (n%2!=0 || n==2)
{
System.out.println(n+" is prime. ");
}
/*
else //if(n%2!=0)
{
System.out.println(n+" is composite. ");
}
*/
n++;
}
while(n<=100);
}
} |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Jan 07, 2008 8:49 am Post subject: |
|
|
Driver?
A driver is something that interacts really low level with the hardware.
Do you mean method?
That's a definable snippet of code used in applications. (Wee, I get to sound smart! =D)
_________________
|
|
| Back to top |
|
 |
|