The time now is 12/02/08 - 04:21
Log in: Username: Password:
Search forums for:
  

What's wrong with this Java Code?!

Post new topic   Reply to topic
Author Message
Mental_Hernia
RealPoor Guru
RealPoor Guru


Joined: 14 Oct 2002
Posts: 3336
Location: Texas



PostPosted: 03/10/04 - 00:38    Post subject: What's wrong with this Java Code?! Reply with quote

Code:

import java.io.*;
class Chap20Ex1
{
  public static void main(String[] args ) throws IOException
  {
    int Lock1 = 6;
    int   Lock1A1 = Lock1 + 1;
    int   Lock1A2 = Lock1 + 2;
    int   Lock1A3 = Lock1 + 3;
    int   Lock1B1 = Lock1 - 1;
    int   Lock1B2 = Lock1 - 2;
    int   Lock1B3 = Lock1 - 3;
       
    int Lock2 = 12;
    int   Lock2A1 = Lock2 + 1;
    int   Lock2A2 = Lock2 + 2;
    int   Lock2A3 = Lock2 + 3;
    int   Lock2B1 = Lock2 - 1;
    int   Lock2B2 = Lock2 - 2;
    int   Lock2B3 = Lock2 - 3;
           
    int Lock3 = 30;
    int   Lock3A1 = Lock3 + 1;
    int   Lock3A2 = Lock3 + 2;
    int   Lock3A3 = Lock3 + 3;
    int   Lock3B1 = Lock3 - 1;
    int   Lock3B2 = Lock3 - 2;
    int   Lock3B3 = Lock3 - 3;
       
    int num;

    BufferedReader stdin = new BufferedReader(
        new InputStreamReader( System.in ) );
    String inData;

    boolean correct;
    correct = false;
   
    System.out.println("Enter first number: ");
    inData = stdin.readLine();
    num  = Integer.parseInt(inData);
   
   if ( num == Lock1 || num == Lock1A1 || num == Lock1A2 || num == Lock1A3 ||   
       num == Lock1B1 || num == Lock1B2 || num == Lock1B3 )
       {
          correct = true;
       }       
       
       
       
       
   System.out.println("Enter second number: ");
    inData = stdin.readLine();
    num  = Integer.parseInt(inData);   
   
    if ( num == Lock2 || num == Lock2A1 || num == Lock2A2 || num == Lock2A3 ||   
       num == Lock2B1 || num == Lock2B2 || num == Lock2B3 )
       {
          correct = true;
       }
       
       
       
   System.out.println("Enter third number: ");
    inData = stdin.readLine();
    num  = Integer.parseInt(inData);
   
   if ( num == Lock3 || num == Lock3A1 || num == Lock3A2 || num == Lock3A3 ||   
       num == Lock3B1 || num == Lock3B2 || num == Lock3B3 )
       {
          correct = true;
          if ( correct )
         {
            System.out.println("Lock does not open");
         }
         else
         {
            System.out.println("Lock opens");
         }
       }                 
   }
 }


What it should do is act like a combination lock.. You type in the 3 numbers, and if any of the numbers are within 3 of the actual number, the combo will work and say ' opens ' if not then it says ' wont open'

i can figure out the loops to get it right.

f**k!
Back to top
kireol
RealPoor Master of Posts
RealPoor Master of Posts


Joined: 02 Aug 2003
Posts: 9517
Location: Royal Oak, MI



PostPosted: 03/10/04 - 00:55    Post subject: Reply with quote

well, you could clean up a lot of code by



if(numlock > lock1 - 3 and numlock < lock1 +3)


for starters
Back to top
Xion
RealPoor Guru
RealPoor Guru


Joined: 11 Oct 2002
Posts: 2117
Location: Los Angeles, CA



PostPosted: 03/10/04 - 00:58    Post subject: Reply with quote

you need to rework your program with kireol's algorithm. your current one is very sloppy.
Back to top
Mental_Hernia
RealPoor Guru
RealPoor Guru


Joined: 14 Oct 2002
Posts: 3336
Location: Texas



PostPosted: 03/10/04 - 00:59    Post subject: Reply with quote

Xion wrote:
you need to rework your program with kireol's algorithm. your current one is very sloppy.


I know that, but I didnt now the code to have a variance between what a variable can equal
Back to top
Xion
RealPoor Guru
RealPoor Guru


Joined: 11 Oct 2002
Posts: 2117
Location: Los Angeles, CA



PostPosted: 03/10/04 - 01:03    Post subject: Reply with quote

it's just a blanket if statement. if x > 1 and x < 5 would give you a variance of 3.

subject the number input to a test by adding 3 to it and subtracting 3 to it.

such as x+3 = y and x-3= z.

then make an if statement for y and z such that they have to land within the numerical range of the combination.
Back to top
kireol
RealPoor Master of Posts
RealPoor Master of Posts


Joined: 02 Aug 2003
Posts: 9517
Location: Royal Oak, MI



PostPosted: 03/10/04 - 01:09    Post subject: Reply with quote

even more pretty would be something like

Code:

......
int variance;
int lock1;
int lock2;
int lock3;

variance = 3;

// get input and store in num

   if (num > lock1 -variance && num < lock1+variance)
   {
          //lock1 was good
   }
   else
   {
        //lock1 failed
   }

.....
Back to top
Xion
RealPoor Guru
RealPoor Guru


Joined: 11 Oct 2002
Posts: 2117
Location: Los Angeles, CA



PostPosted: 03/10/04 - 01:10    Post subject: Reply with quote

you also need elses for your first 2 if statements so if the first test fails it tells the user to try the first number again.
Back to top
Xion
RealPoor Guru
RealPoor Guru


Joined: 11 Oct 2002
Posts: 2117
Location: Los Angeles, CA



PostPosted: 03/10/04 - 01:12    Post subject: Reply with quote

you're lucky kireol's not lazy like me and wrote code out for you, hehe.

those should be greaterthan/less than OR EQUAL TO signs, btw.
Back to top
Mental_Hernia
RealPoor Guru
RealPoor Guru


Joined: 14 Oct 2002
Posts: 3336
Location: Texas



PostPosted: 03/10/04 - 01:15    Post subject: Reply with quote

Code:

import java.io.*;
class Chap20Ex1
{
  public static void main(String[] args ) throws IOException
  {
    int Lock1 = 6;       
    int Lock2 = 12;
    int Lock3 = 30;
    int num;
    int variance;
   
    variance = 3;

    BufferedReader stdin = new BufferedReader(
        new InputStreamReader( System.in ) );
    String inData;

    boolean correct;
   
    correct = true;

    //First Number
    System.out.println("Enter first number: ");
    inData = stdin.readLine();
    num  = Integer.parseInt(inData);

    if ( num > Lock1 + variance || num < Lock1 - variance)   
      {
         correct = false;
      }
     
    System.out.println("Enter second number: ");
    inData = stdin.readLine();
    num  = Integer.parseInt(inData);

    if ( num > Lock2 + variance || num < Lock2 - variance)   
      {
         correct = false;
      }
     
    System.out.println("Enter first number: ");
    inData = stdin.readLine();
    num  = Integer.parseInt(inData);

    if ( num > Lock3 + variance || num < Lock3 - variance)   
      {
         correct = false;
      }
     
      if ( correct )
      {
         System.out.println("Lock opens");
      }
      else
      {
         System.out.println("Lock doesn't open");
      }
   }
}


Holy crap, you both are f*****g insane.
Back to top
Mental_Hernia
RealPoor Guru
RealPoor Guru


Joined: 14 Oct 2002
Posts: 3336
Location: Texas



PostPosted: 03/10/04 - 01:16    Post subject: Reply with quote

Xion wrote:
you also need elses for your first 2 if statements so if the first test fails it tells the user to try the first number again.


I see why you would say that, but the instrucitons don't include that, or else I would!
Back to top
Occulis
RealPoor Jedi
RealPoor Jedi


Joined: 11 Oct 2002
Posts: 13293
Location: Moral Relativity Central



PostPosted: 03/10/04 - 08:57    Post subject: Reply with quote

It's a trick question. The #1 problem with the code is there are no comments and no documentation. Rewrite it in some kind of PDL or pseudocode and get back to me by 2pm!
Back to top
Filamil
Rookie
Rookie


Joined: 11 Oct 2002
Posts: 81



PostPosted: 03/10/04 - 11:01    Post subject: Reply with quote

The problem is it's in java.
Back to top
Display posts from previous:   
Post new topic   Reply to topic
Page 1 of 1

Related topics:
hey what's up (NWS maggot breast)
What's the 'teen' forum?
What's a Mischa Barton?
What's your every day necessity? ? ? ? ? ? ? ? ? ?
what's the name of the hamster in south park
What's up?
What's better?
What's the deal with people being invisible?
What's Star Trek Online about?
What's the song
what's all this 'sir' bullshit
What's On Your Player Today?
What's the link to that funny Opal commercial?
What's going on in the car forums?
So what's up with Ikkan and a couple other
What's happened to MoKB lately?
What's the best way to avoid a scammer?
What's the best way to explore Bora Bora?
What's new?
hahah what's up with seddam's glasses!