Computer Science Canada

tiny problem, come pros!!!

Author:  MingYang [ Wed Dec 14, 2005 6:50 pm ]
Post subject:  tiny problem, come pros!!!

hi this programgenerates a random integer between 1 and 100. After each incorrect guess, the program should tell the user if they guessed too high or too low.

Here's the code, for some reason it only starts counting on the second input that user provide. Note: I have Int.java thingy so i dont need to write crazy code for Int.get etc.

code:
/**
 * This program will allow the user to guess a number between 1
 * to 100, program will stop when the user guessed the correct number.
 * @Author: Ming Yang
 * @December 12, 2005
 */

class Number {  //setup the class name for the program
 public static void main(String[] args){    // the main method is always run first
   int number, number2, count;
   number = (int)(100 *Math.random ());
 
    System.out.print("Enter # from 1-100: \n");
    count = 0;
    number2 = In.getInt();
    while (number2 != number) {
    number2 = In.getInt();
 
    count = count + 1;
    System.out.print("\nYou have guessed " + count + " time(s). \n");
   
      if (number>number2) {
       System.out.print("THE NUMBER IS TOO LOW");
     }
       else if (number<number2) {
       System.out.print ("THE NUMBER IS TOO HIGH");
       }
       else {
       System.out.print ("GOOD JOB YOU GOT IT IN " + count + " TRIES.");
     }
     }
}
}


thanks

Author:  wtd [ Wed Dec 14, 2005 7:19 pm ]
Post subject: 

First remove the extraneous comments, and then use consistent indenting.

Author:  MingYang [ Wed Dec 14, 2005 7:34 pm ]
Post subject: 

taking off the comment wont do anything and tried to indent but still same result.

Author:  wtd [ Wed Dec 14, 2005 7:38 pm ]
Post subject: 

But it will make it possible for people to actually read your code. And you should be writing code with proper formatting and without extraneous comments anyway.

Author:  MingYang [ Wed Dec 14, 2005 7:47 pm ]
Post subject: 

code:

class Number {
 public static void main(String[] args){
   
    int number, guess, count;
    number = (int)(100 *Math.random ());

    System.out.print("Enter # from 1-100: \n");
   
    count = 0;
    guess = In.getInt();
    while (guess != number) {
    guess = In.getInt();
   
    count = count + 1;
    System.out.print("\nYou have guessed " + count + " time(s). \n");
   
      if (number>guess) {
       System.out.print("THE NUMBER IS TOO LOW");
       }
       else if (number<guess) {
       System.out.print ("THE NUMBER IS TOO HIGH");
       }
       else {
       System.out.print ("GOOD JOB YOU GOT IT IN " + count + " TRIES, THE NUMBER WAS " + guess);
       }
     }
   }
}


Author:  wtd [ Wed Dec 14, 2005 7:53 pm ]
Post subject: 

Not even close. Anytime you enter a block (between curly braces) you should indent.

Author:  md [ Wed Dec 14, 2005 7:56 pm ]
Post subject: 

The solution is really really obvious if you look at your code too...

Author:  MingYang [ Wed Dec 14, 2005 10:20 pm ]
Post subject: 

wtd wrote:
Not even close. Anytime you enter a block (between curly braces) you should indent.


seriously you're not helpign at all, you keep on talking about my indentation, well i came here to ask help on my coding part, the last time I was in mIRC and i asked some NOOBIE questions and you were saying about the indentation too. For being a big noob i am guess i'm not worthy of your time anyways.


Quote:
The solution is really really obvious if you look at your code too...


i just fixed it by using the Post-Test instead of Pre-Test. thanks for your comment.

code:
class Number {  //setup the class name for the program
  public static void main(String[] args){    // the main method is always run first
   
   int number, guess, count;     //three variables as int
   
   number = (int)(100 *Math.random ());      //the variable that is generated as an int value from 1-100
   System.out.print("Enter # from 1-100: \n");
   
   count = 0;   //# of guess starts from 0
   do {   //start of the loop
     guess = In.getInt();     //it will prompt the user again until the user gets it right
     count = count + 1;     //counter increases by  as the user guess wrong each time
     System.out.print("\nYou have guessed " + count + " time(s). \n");
     
     if (number>guess) {
        System.out.print("THE NUMBER IS TOO LOW");
     }
     else if (number<guess) {
       System.out.print ("THE NUMBER IS TOO HIGH");
     }
     else {
       System.out.print ("GOOD JOB YOU GOT IT IN " + count + " TRIES, THE NUMBER WAS " + guess);
     }
   } while (guess != number); //if guess number is not equal to number it'll go back form do
  }
}

Author:  wtd [ Wed Dec 14, 2005 10:49 pm ]
Post subject: 

MingYang wrote:
wtd wrote:
Not even close. Anytime you enter a block (between curly braces) you should indent.


seriously you're not helpign at all, you keep on talking about my indentation, well i came here to ask help on my coding part, the last time I was in mIRC and i asked some NOOBIE questions and you were saying about the indentation too. For being a big noob i am guess i'm not worthy of your time anyways.


Getting it wrong isn't a problem. We all make mistakes. I've made more than my share over the years.

But not learning from your mistakes is a bad thing.

Additionally, you go ahead and throw in even more extraneous comments. You should not write in comments what the code already specifies quite clearly, such as:

code:
   int number, guess, count;     //three variables as int


I don't point these things out to be a jerk. I point them out because these are very basic skills which, if you learn them now, will save you loads of time in the future and let you concentrate on more important matters.

Author:  McKenzie [ Wed Dec 14, 2005 10:52 pm ]
Post subject: 

Ming, why not just start from the assumtion that if wtd bothers to type an answer that he is doing it for your benefit? He has making a simple comment "your code is a mess, no one is going to bother to look at it and it is going to be hard for you to find the error on your own. I know you were hoping for a quick fix but the style problem is actually a more serious one than the little logic problem you were having (I know it doesn't seem that way at the time.) You can agree with wtd's methods and opinions or not but don't be upset at him for not giving you the answer you want. He is looking to give you the answer you need.

Author:  wtd [ Wed Dec 14, 2005 10:59 pm ]
Post subject: 

Thank you. I think you stated that much better than I did.

Author:  md [ Wed Dec 14, 2005 11:16 pm ]
Post subject: 

And for the record, those here at compsci who are most able to answer questions are usually in agreement with wtd's opinion on code style. Readability is just as important as logic when trying to find errors.

Author:  MingYang [ Wed Dec 14, 2005 11:19 pm ]
Post subject: 

[quote="wtd"]
MingYang wrote:
wtd wrote:
Not even close. Anytime you enter a block (between curly braces) you should indent.
=

Additionally, you go ahead and throw in even more extraneous comments. You should not write in comments what the code already specifies quite clearly, such as:

code:
   int number, guess, count;     //three variables as int




teacher wants them -.-||

Author:  wtd [ Wed Dec 14, 2005 11:25 pm ]
Post subject: 

Then your teacher, quite frankly, is teaching you a bad style.

Author:  wtd [ Wed Dec 14, 2005 11:26 pm ]
Post subject: 

Cornflake wrote:
And for the record, those here at compsci who are most able to answer questions are usually in agreement with wtd's opinion on code style. Readability is just as important as logic when trying to find errors.


Messy code hides errors.

Extraneous comments increase the chances of the actual code falling out of sync with the comments. One might then read the comments and be mislead about what the code is doing.

Author:  [Gandalf] [ Thu Dec 15, 2005 4:59 pm ]
Post subject: 

I recently looked into another class, and I must say, Ming isn't the only one in this situation. Not-so-knowledgable-in-this-aspect teachers quite often make comments such as "document your program properly" when this is often taken, and accepted as adding in these "extraneous" comments. It doesn't help when the "IDE" you are using writes these extraneous comments for you at the start. More on that later.

Author:  MysticVegeta [ Wed Jan 18, 2006 11:57 pm ]
Post subject: 

I would have to agree with wtd and Mr Mckenzie, because there were lot of times I mean a LOT of times when these things happened to me, I wasnt able to solve the probelm because I couldn't see it. But the habit of indentation got into me, so I have put myself into this habit of pressing F2(indent) before F1(run). Everytime I run my program F2 is always there first. I never knew I could benefit so much from just indenting the code. Very Happy

Author:  Andy [ Thu Jan 19, 2006 11:03 am ]
Post subject: 

wtd wrote:
Then your teacher, quite frankly, is teaching you a bad style.


oh wtd.. i took cs134 at waterloo last year, and the only time i didnt get docked any marks on documentation is when i commented every lines.. even the else, some people are ridiculous, and you have to do what they ask or you get slapped.

Author:  wtd [ Thu Jan 19, 2006 2:22 pm ]
Post subject: 

Andy wrote:
wtd wrote:
Then your teacher, quite frankly, is teaching you a bad style.


oh wtd.. i took cs134 at waterloo last year, and the only time i didnt get docked any marks on documentation is when i commented every lines.. even the else, some people are ridiculous, and you have to do what they ask or you get slapped.


Then go over the professor's head. Go to the head of the department. Tell them what's being asked and how it's not only pointless, but instills habits you'll have to break later. If they won't listen, go to their boss.

Author:  Andy [ Thu Jan 19, 2006 2:35 pm ]
Post subject: 

you know what, if that happens again, i will.


: