Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


code problem (scripts included~change)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
lexie
Advanced Cheater
Reputation: 0

Joined: 23 Jan 2007
Posts: 80

PostPosted: Fri Jan 04, 2008 11:38 pm    Post subject: code problem (scripts included~change) Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Jan 05, 2008 7:22 am    Post subject: Re: code problem (scripts included~change) Reply with quote

You do this:

Code:

if (menu == '1');


'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:

Code:

if (menu == 1)


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.");
}

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
lexie
Advanced Cheater
Reputation: 0

Joined: 23 Jan 2007
Posts: 80

PostPosted: Mon Jan 07, 2008 12:21 am    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Mon Jan 07, 2008 8:49 am    Post subject: Reply with quote

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)

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites