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?