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!

Edited 3 Years Ago by Dani: Formatting fixed

er, how do i do that?

"CODE" put your code here "/CODE"

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

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);

		}
}

Edited 7 Years Ago by estefania: n/a

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.

Edited 7 Years Ago by Grn Xtrm: n/a

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();

	}

Edited 7 Years Ago by Jocamps: n/a

Comments
Good team effort. Great working with you :)
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");
    }
}

Edited 7 Years Ago by quuba: n/a

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 );
This article has been dead for over six months. Start a new discussion instead.