954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

what's wrong eith my coding?

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?

rayda
Newbie Poster
22 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

post the errors you are getting

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

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

rayda
Newbie Poster
22 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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'.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You