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!