I am suppose to move the code from the 1st program that actually plays the game into a new method. I am really new at this and have no idea...

import java.util.Scanner;

public class GuessingGame
{
    public static void main(String [] args)
    {

        Scanner scan= new Scanner(System.in);
        int n;  //a random computer generated number
        int guess;  //a number that the user types  
        String reply;   //user's decision to replay the game or not. 
        String Y, N;    //Answer that the user types that will restart the loop or end the program.


        do  
        {
        n= (int) (Math.random()*100.0)+1;   //Math equation to generate random number
            do
            {
        System.out.println("Enter number between 0-100: ");
        guess= scan.nextInt();


            if (guess > n)
            System.out.println("Sorry, too high.");

            if (guess < n)
            System.out.println("Sorry, too low.");  
            }               

            while ( guess != n );

                System.out.println("Congrats you have guessed the correct answer!");

                System.out.println("Do you want to play again? [Y/N]");
                reply=scan.next();
                }

                while (reply.equals("Y"));

                    System.out.println("Thank you for playing!");

    }

}

Is this how you do it? It still doesnt work for me.

import java.util.Scanner;

public class Guess
{
        public static void getNumber(int guess);
    {
        int n;  //a random computer generated number


            n= (int)(Math.random()*100.0)+1;    //Math equation to generate random number

                if (guess > n)
                System.out.println("Sorry, too high.");

                if (guess < n)
                System.out.println("Sorry, too low.");  
            }               

             while ( guess != n );

                System.out.println("Congrats you have guessed the correct answer!");
        }       
        public static int main (String [] args)
        {
            Scanner scan= new Scanner(System.in);
            System.out.println("Enter number between 0-100: ");
            guess= scan.nextInt();
            getNumber(guess);

        }       
}

thanks!

please enclose you code inside the CODE tags so its easier to read.

er, how do i do that?

er, how do i do that?

"CODE" put your code here "/CODE"

use that syntax above and replace the quotation marks with square brackets [ ].

how do i edit it?

Just repost the code using the code tags this time.

this is the 1st program i need to extract the method from

import java.util.Scanner;

public class GuessingGame
{
	public static void main(String [] args)
	{
	
		Scanner scan= new Scanner(System.in);
		int n;	//a random computer generated number
		int guess;	//a number that the user types	
		String reply;	//user's decision to replay the game or not. 
		String Y, N;	//Answer that the user types that will restart the loop or end the program.
		
		
		do	
		{
		n= (int) (Math.random()*100.0)+1;	//Math equation to generate random number
			do
			{
		System.out.println("Enter number between 0-100: ");
		guess= scan.nextInt();
		

			if (guess > n)
			System.out.println("Sorry, too high.");
			
			if (guess < n)
			System.out.println("Sorry, too low.");	
			}				
				
			while ( guess != n );
			
				System.out.println("Congrats you have guessed the correct answer!");
			
				System.out.println("Do you want to play again? [Y/N]");
				reply=scan.next();
				}
	
				while (reply.equals("Y"));
				
					System.out.println("Thank you for playing!");
			
	}
	
}

and i need to take the part that plays the game and put it to a new method. This is what i have

import java.util.Scanner;

public class Guess
{
	public static void getNumber(int guess)
	{
		int n, number;	//a random computer generated number
				
			do
			{
			Scanner scan= new Scanner(System.in);
			System.out.println("Enter number between 0-100: ");
			number= scan.nextInt();
			
			n= (int)(Math.random()*100.0)+1;	//Math equation to generate random number

				if (number > n)
				System.out.println("Sorry, too high.");
			
				if (number < n)
				System.out.println("Sorry, too low.");	
			}				
			 while ( number != n );
			
				System.out.println("Congrats you have guessed the correct answer!");
		}		
 		public static void main (String [] args)
		{
		Scanner scan= new Scanner(System.in);
		System.out.println("Enter number between 0-100: ");
				int number= scan.nextInt();
				getNumber(number);

		}
}

First I would try to create a method in your second code that is essentially a copy of the main method in the first code. Try making it a void function with no parameters. Then just call it in the main method of the second code. I'm not positive that this will work flawlessly but I think it is worth a shot. Good luck.

thanks so much, it works better already but problem now is that when i enter the number, the "too high" and "too low" parts dont work.

import java.util.Scanner;

public class Guess
{
	public static void getNumber()
	{

		int n, number;	//a random computer generated number
				
			do
			{
			Scanner scan= new Scanner(System.in);
			System.out.println("Enter number between 0-100: ");
			number= scan.nextInt();
			
			n= (int)(Math.random()*100.0)+1;	//Math equation to generate random number

				if (number > n)
				System.out.println("Sorry, too high.");
			
				if (number < n)
				System.out.println("Sorry, too low.");	
			}				
			 while ( number != n );
			
				System.out.println("Congrats you have guessed the correct answer!");
		}		
 		public static void main (String [] args)
		{
			getNumber();

		}
}

Again, not sure but try changing line 20 to else if, instead of just if.

it still doesn't work :(

You can just copy everything you had inside the main method (in the first code) and paste it to your getNumber() method without any parameters as what Grn Xtrm suggested. Since you declare the method as static, you can call it in the new main method as Guess.getNumber()

your second main method should look like this:

public static void main (String [] args)
	{
		Guess.getNumber();

	}
commented: Good team effort. Great working with you :) +1
public class GuessingGame {

    public static void main(String[] args) {
        while (game()) {
        }
        System.out.println("Thank you for playing!");
    }

    public static boolean game() {
        Scanner scan = new Scanner(System.in);
        int n;	//a random computer generated number
//................................
            System.out.println("Do you want to play again? [Y/N]");
            reply = scan.next();
        } while (reply.equals("Y"));
        return reply.equals("Y");
    }
}

Actually the problem is you generated a new random number every time the user inputs a guess since the statement that this was inside the do-while loop. Try putting it outside the loop.

n= (int)(Math.random()*100.0)+1; //Math equation to generate random number
 do{
...
}			
 while ( number != n );

hey, thanks, I managed to figure it out =)

Estefania, please mark the thread solved if your problem has been solved. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.