JAVA help: NUMBER GUESSING HELP diff then what you think

Discussion in 'Mac Programming' started by neelvaka, Sep 15, 2007.

  1. neelvaka macrumors newbie

    Joined:
    Sep 15, 2007
    #1
    ok this is my current code
    Code:
    //I'm going to use these from now own. Help organize thoughts more for me.
    
    import java.util.Scanner;
    public class quadratic
    {
    	public static void main(String[] args)
    {
    		Scanner kb = new Scanner(System.in); //this is 4 user input
    		int x = (int)(100 * Math.random()) + 1; 
    		int inputnumber = 0; //this is the user input #
    		char answer;
    		int counter = 0;
    		boolean tf = true;
    
    			System.out.print("If you wnna play a game say yes otherwise say no: ");
    			String answer2 = kb.nextLine();
    			answer = answer2.charAt(0);
    
    			while (tf == true)
    {
    				char answer3 = 'y'; //Compares yes and no answer from the user yepyep
    
    				if (answer != answer3)
    {
    					System.out.println("Goodbye!");
    					tf = false;
    }
    				else
    {
    					System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n"); //and the guessing game starts!
    
    					while (x != inputnumber)
    {
    						System.out.print("What's your guess? ");
    						inputnumber = kb.nextInt();
    
    						if (x < inputnumber)
    {
    							System.out.println(inputnumber + " is too big!\n");
    }
    
    						else if (x > inputnumber)
    {
    							System.out.println(inputnumber + " is too small!\n");
    }
    						counter = counter + 1;
    }
    					if (counter == 1)
    {
    						System.out.println("You've got it in " + counter + " guesses. That was lucky!");
    						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    						break;
    }
    					else if (counter >= 2 && counter <= 4)
    {
    						System.out.println("You've got it in " + counter + " guesses. That was amazing!");
    						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    						break;
    }
    					else if (counter >= 5 && counter <= 6)
    {
    						System.out.println("You've got it in " + counter + " guesses. That was really good!");
    						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    						break;
    }
    					else if (counter == 7)
    {
    						System.out.println("You've got it in " + counter + " guesses. That was ok!");
    						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    						break;
    }
    					else if (counter >= 8 && counter <= 9)
    {
    						System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
    						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    						break;
    }
    					else
    {
    						System.out.println("You've got it in " + counter + " guesses. This is not your game!");
    						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    						break;
    }
    }
    }
    }
    }
    and it is right nothing wrong whatsoever. IT WORKS!

    BUT

    after reading my assignment carefully we werent supposed to use scanner but javaIO.* :eek::eek::eek:nd that is diff then what im used to :confused:

    help me out ppl plz?

    btw new to this forum and it rocks lol!
    pretty much i need to use strings and readLines and stuff
    but im really confused:confused:
    any help would be great!


    btw i am using ECLIPSE PLATFORM to run these scripts if that helps and im not allowed to use anything else
    http://www.eclipse.org/
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Well I suggest reading up on BufferedReader, InputStreamReader, and StringTokenizer to find a solution. I'd say more, but this is your assignment so you should making the effort. Good luck.
     
  3. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #3
    Is your task to implement your own methods equivalent to the nextLine and nextInt methods of the Scanner class?

    If so, the starting point for doing that is to make sure you know what they do:
    nextLine reads the InputStream (starting where it left off) until the next linebreak and returns what was read as a string.

    nextInt reads the InputStream (starting where it left off), skipping whitespace, to find an integer (a sequence of digits with an optional leading + or -) and returning the int value.​
    Does that make sense? Is that enough to get you started?
     
  4. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #4
    you should justify the curly braces with the code they go with. that's the second most ridiculous convention i've seen.
     
  5. Texas04 macrumors 6502a

    Texas04

    Joined:
    Jul 2, 2005
    Location:
    Texas
    #5
    Java IO?

    Does he want you to read in from a file?

    And also remember formatting issues.

    For the if statements and while and everything that needs brackets, do something like this.


    Code:
    while(thisVariable==thatVariable)
       {
              if(theWord=!thatWord)
              {
                       System.out.println("Do this");
              }
       }
       else
       {
              if(thisWord==thatWord)
              {    
                     System.out.prtinln("Do that");
              }    
       }
    If you use the tab function it will look a lot nicer then mine on these forums :).
     
  6. btaussie macrumors member

    btaussie

    Joined:
    Jun 11, 2007
    #6
    Say thanks :p

    //Here you go mate, you owe me one :)

    import java.io.*;
    public class Quadratic
    {
    public static void main(String[] args)
    {
    BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
    int x = (int)(100 * Math.random()) + 1;
    int inputnumber = 0;
    char answer;
    int counter = 0;
    boolean tf = true;

    System.out.print("If you wanna play a game say yes otherwise say no: ");
    String answer = kb.readLine().charAt(0);
    //answer = answer2.charAt(0);

    while (tf == true)
    {
    char answer3 = 'y'; //Compares yes and no answer from the user yepyep

    if (answer != answer3)
    {
    System.out.println("Goodbye!");
    tf = false;
    }
    else
    {
    System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n"); //and the guessing game starts!

    while (x != inputnumber)
    {
    System.out.print("What's your guess? ");
    inputnumber = Integer.parseInt(kb.readLine());

    if (x < inputnumber)
    {
    System.out.println(inputnumber + " is too big!\n");
    }

    else if (x > inputnumber)
    {
    System.out.println(inputnumber + " is too small!\n");
    }
    counter = counter + 1;
    }
    if (counter == 1)
    {
    System.out.println("You've got it in " + counter + " guesses. That was lucky!");
    System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    break;
    }
    else if (counter >= 2 && counter <= 4)
    {
    System.out.println("You've got it in " + counter + " guesses. That was amazing!");
    System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    break;
    }
    else if (counter >= 5 && counter <= 6)
    {
    System.out.println("You've got it in " + counter + " guesses. That was really good!");
    System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    break;
    }
    else if (counter == 7)
    {
    System.out.println("You've got it in " + counter + " guesses. That was ok!");
    System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    break;
    }
    else if (counter >= 8 && counter <= 9)
    {
    System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
    System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    break;
    }
    else
    {
    System.out.println("You've got it in " + counter + " guesses. This is not your game!");
    System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
    break;
    }
    }
    }
    }

    //I'll leave you to format your code correctly but if you are using popular Java editors like Eclipse/NetBeans, you can use automatic settings that will format your code for you! Google it :)
     
  7. btaussie macrumors member

    btaussie

    Joined:
    Jun 11, 2007
    #7
    Forgot to add, does your code need to capture exceptions? Specifically IOExceptions, NumberFormatExceptions etc....
     
  8. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #8
    A caution

    We prefer to have members give advice, not complete solutions, to those doing homework, but it's up to the individuals involved.

    Be aware that we've had cases where professors or other students visited online forums and identified students who used code written for them by others.
     
  9. btaussie macrumors member

    btaussie

    Joined:
    Jun 11, 2007
    #9

    That's fair enough, but if you look at the code, I changed three lines of code, that's it.

    Enjoy!
     
  10. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #10
    I didn't mean to direct my comment at you. Just wanted to remind everyone, for everyone's benefit.
     
  11. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #11
    This guy deserves to fail his course and even his degree. If the first thing he thinks of when confronted by a problem is to automatically post on a forum rather than trying to look through his books or at programming examples, can you imagine how useless this guy would be in a job.
     
  12. btaussie macrumors member

    btaussie

    Joined:
    Jun 11, 2007
    #12
    Dude if you have any idea about Java Programming you can tell this guy has still got a hell of a lot to learn about programming in Java before you could make an assumption that "...useless this guy would be in a job." My bet is this guy is in his first semester of programming at college/university. Still got a long way to go. Although I don't believe the guy deserves to fail his course and even his degree, I do believe consulting notes, texts and even websites are the way to go before posting 'automatically' on forums.

    That's what helped me get my degree and is sure helping me complete my Masters at the moment.

    Cheers.
     
  13. btaussie macrumors member

    btaussie

    Joined:
    Jun 11, 2007
    #13
    It's all good mate. Buy me an iPhone and we're all sweet. :rolleyes:

    Cheers.
     

Share This Page