import javax.swing.JOptionPane;
import java.util.Random;

public class guess2
{
	public static void main(String[]args)
	{
		int num;
		int guess;
		int diff;
		String input;
		int Diff;


		Random randomNumbers=new Random();
		guess=randomNumbers.nextInt(100);
		diff= num - guess;
		Diff=Math.abs(diff);

		input=JOptionPane.showInputDialog("Enter your guess.");
		num=Integer.parseInt(input);


		for(int i=0; i<5;i++)
		{
			while(num<0 || num>100)
			{
				input=JOptionPane.showInputDialog("Invalid number!Please re-enter: ");
				num=Integer.parseInt(input);

				if(Diff==0)
				{
					JOptionPane.showMessageDialog(null,"Correct!");
				}
				else if(Diff>=50)
				{
					if(num>guess)
					JOptionPane.showMessageDialog(null,"Your guess is very high!");
					else if(num<guess)
					JOptionPane.showMessageDialog(null,"Your guess is very low!");
				}
				else if(Diff>=30 && Diff<50)
				{
					if(num>guess)
					JOptionPane.showMessageDialog(null,"too high");
					else if(num>guess)
					JOptionPane.showMessageDialog(null,"too low");
				}
				else if(Diff>=15 && Diff<30)
				{
					if(num>guess)
					JOptionPane.showMessageDialog(null,"high!");
					else if(num<guess)
					JOptionPane.showMessageDialog(null,"low!");
				}
				else if(Diff>0 && Diff<15)
				{
					if(num>guess)
					JOptionPane.showMessageDialog(null,"high!");
					else if(num<guess)
					JOptionPane.showMessageDialog(null,"low!");
				}
			}
		}
	}
}

this is a guessing number program.
i need to include Math.abs in it.
but what's wrong with my coding?..
why i kept get error?

post the errors you are getting

this is the error that i get:
java\assignment\guess2.java:17: variable num might not have been initialized
diff= num - guess;
^
1 error

Tool completed with exit code 1

You are trying to use `num' before initializing it i.e. providing it an initial value which works for member variables which have a default value but doesn't for local variables which don't.

Move the two statements which ask the user for input and parse `num' before the statement which first uses `num'.

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.