Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Methods

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Methods

    Hi! I'm fairly new to Java and as ridiculous as this sounds, I'm having a difficult time wrapping my head around methods and how to execute them.
    For example, right now I'm making a game for class where the user guesses a number that a random generator pulls up. It's in one class right now called numbersTester but I want to have two classes (numbers and numbersTester) where numbers have the methods. I attempted to do this myself but I didn't know how to pull up the code in numbersTester correctly so I just put it in one class.
    Any feedback/help would be appreciated!

    import java.util.Random;
    import java.util.Scanner;

    public class NumbersTester extends Numbers {

    public static void main(String[] args) {

    Random number = new Random();
    int numberToGuess=number.nextInt(100);
    Scanner input = new Scanner (System.in);
    boolean win = false;

    int numberOfTries = 0;
    int guess;

    while (win == false){
    System.out.println("Guess a number between 1 and 100: ");
    guess = input.nextInt();
    numberOfTries ++;

    if (guess == numberToGuess){
    win = true;
    }

    if (guess > 100 || guess < 0){
    System.out.println("Invalid number!");
    }

    else if (guess < numberToGuess){
    System.out.println("The number is too low! Guess again.");
    }
    else if (guess > numberToGuess){
    System.out.println("The number is too high! Guess again.");
    }

    }

    System.out.println("\n" + "Game Over! " + "\n" + "You guessed correctly.");
    System.out.println("The answer was: " + numberToGuess);
    System.out.println(numberOfTries + " tries.");

    }

    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Methods

    You already know how to do this. I imagine you have used Strings before and called many of its methods. In the code above you have a Random object and a Scanner object and manage to call methods of those classes. So why does it have to be so difficult when you write your own class?
    Improving the world one idiot at a time!

Similar Threads

  1. Methods help????
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 24
    Last Post: February 27th, 2013, 01:21 PM
  2. Methods
    By kave2 in forum Java Theory & Questions
    Replies: 8
    Last Post: January 22nd, 2013, 11:56 AM
  3. [SOLVED] Help with Methods
    By Blasfemmy in forum What's Wrong With My Code?
    Replies: 24
    Last Post: October 22nd, 2012, 08:18 PM
  4. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  5. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM

Tags for this Thread