Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 tiny problem, come pros!!!
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MingYang




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Dec 14, 2005 7:19 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 14, 2005 7:34 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 14, 2005 7:38 pm   Post subject: (No 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.
MingYang




PostPosted: Wed Dec 14, 2005 7:47 pm   Post subject: (No 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);
       }
     }
   }
}

wtd




PostPosted: Wed Dec 14, 2005 7:53 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 14, 2005 7:56 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 14, 2005 10:20 pm   Post subject: (No 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
  }
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Dec 14, 2005 10:49 pm   Post subject: (No 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.
McKenzie




PostPosted: Wed Dec 14, 2005 10:52 pm   Post subject: (No 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.
wtd




PostPosted: Wed Dec 14, 2005 10:59 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 14, 2005 11:16 pm   Post subject: (No 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.
MingYang




PostPosted: Wed Dec 14, 2005 11:19 pm   Post subject: (No 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 -.-||
wtd




PostPosted: Wed Dec 14, 2005 11:25 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 14, 2005 11:26 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: