I am doing my homework =D
Hi I just had a quick question. I have this code
if (answer > num1)
JOptionPane.showMessageDialog(null,"Your answer is too high");
else if (answer < num1)
JOptionPane.showMessageDialog(null, "Your answer is too low");
else
JOptionPane.showMessageDialog(null, "You won!");
and its gave me this as an error.
error:bad operand types for binary operator '>'
error:bad operand types for binary operator '<'
What do those errors mean? :confused:
Re: I am doing my homework =D
If you have a question, provide an SSCCE that demonstrates what you're talking about.
For example, what are answer and num1? Are they ints? Chars? Strings? Something else? What are their values?
Re: I am doing my homework =D
They are this
int num1 = (int) (System.currentTimeMillis()%10);
String answer = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
Re: I am doing my homework =D
Okay, so answer is a String. How can a String be less than or greater than a number? Looks like you need to convert that to an int.
Re: I am doing my homework =D
I guess that would make sense to convert into an int.
How about this?
String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
int answer = Integer.parseInt(answerString);
Re: I am doing my homework =D
Quote:
Originally Posted by
valenciajv
I guess that would make sense to convert into an int.
How about this?
String answerString = JOptionPane.showInputDialog(null,"Guess a number between 1 and 10");
int answer = Integer.parseInt(answerString);
You tell me! What happened when you tried it?
Re: I am doing my homework =D
It said process completed. =D
Thank you.
I am not done with the whole thing yet. Time to resume my homework. =D
Re: I am doing my homework =D
Quote:
Originally Posted by
valenciajv
It said process completed. =D
Thank you.
I am not done with the whole thing yet. Time to resume my homework. =D
Well you can always test that piece by adding some print statements, or trying to add numbers to it or by using the < or > operators, and seeing if the results are what you'd expect. A good rule of thumb while programming is to always test the current step before moving on to the next one. That way you always know where any bugs are. But I have a feeling you've got this step down! :)
Re: I am doing my homework =D
Does this make a random number between 1 and 10?
I never exactly knew how this one works cause I would get zeros as well. Can you help?
int num1 = (int) (System.currentTimeMillis()%10);
Re: I am doing my homework =D
Quote:
Originally Posted by
valenciajv
Does this make a random number between 1 and 10?
I never exactly knew how this one works cause I would get zeros as well. Can you help?
int num1 = (int) (System.currentTimeMillis()%10);
The answer to most of these kinds of questions is going to be- what happened when you tried it? System.out.println() is your best friend. Use it.
You might also want to look at the Math or Random classes. The API is your other best friend: Java Platform SE 6