How do I check if an integer divides evenly? I know I have to use the % symbol. The thing I thought about using was %>1 but I don't think you can do that.
Printable View
How do I check if an integer divides evenly? I know I have to use the % symbol. The thing I thought about using was %>1 but I don't think you can do that.
What have you tried with the % symbol? It's called the mod operator, by the way.
Code:public static void main (String[ ] args)
{
Scanner sc = new Scanner(System.in);
int firstNumber;
System.out.println("Input a number");
firstNumber = sc.nextInt();
int secondNumber;
System.out.println("Input another number");
secondNumber = sc.nextInt();
} //======================
}
that's what I have riht now
The mod operator gives you the rest of a division... if it divides evenly you have how much left?
You may just check the result by using the mod operator like a division sign and see what the results are if you do not know how to proceed.
got it, thank you! I also have a math question.
If I had a game where I pick a number and tell you that the number you guessed is hot (2 or less to my number), warm (5 to 3 less to my number) how would I write that mathimatically?
yourNumber = 3;
myNumber = 5;
warmNumber = 5;
hotNumber = 2;
wNGuess =< myNumber + 5;
hNGuess =< mynumber + 2;
Would that work?
You would create the difference between the two numbers and use Math.abs()on the result to determine how far one number is from another. Zero is equal.
You can tell classes are starting up again. I think I've seen this example or one very close to it a lot over the last week.
Exercise 4.27* Revise the GuessNumber game to say "you're hot" if within 2 of the right answer, "you're warm" if within 6 of the right answer, and "too high" or "too low" otherwise.
showUpdatedStatus() is what has been revised. Whenever I play the game and I guess it only says "You're hot!" and I'm not even 2 away! Once I was 11 away. I don't know what to do.Code:import javax.swing.JOptionPane;
public class GuessNumber extends BasicGame {
private java.util.Random randy;
private int itsSecretNumber;
private int itsUsersNumber;
public GuessNumber(){
super();
randy = new java.util.Random();
} //=======================
public void askUsersFirstChoice(){
itsSecretNumber = 1 + randy.nextInt (10);
askUsersNextChoice();
} //=======================
public void askUsersNextChoice(){
String s = JOptionPane.showInputDialog
("Guess my number from 1 to 100:");
if (s != null && ! s.equals ("")) {
itsUsersNumber = Integer.parseInt (s);
}else{
itsUsersNumber = -1; // just to have a value there
}
} //======================
public boolean shouldContinue(){
return itsUsersNumber != itsSecretNumber;
} //======================
//Exercise 4.28
public void showUpdatedStatus(){
if (itsSecretNumber <= itsUsersNumber + 2 || itsSecretNumber >= itsUsersNumber - 2){
JOptionPane.showMessageDialog(null, "You're hot!");
}else
if (itsSecretNumber <= itsUsersNumber + 5 || itsSecretNumber >= itsUsersNumber - 5){
JOptionPane.showMessageDialog(null, "You're warm.");
}
if (itsUsersNumber > itsSecretNumber)
JOptionPane.showMessageDialog (null, "Too high");
else
JOptionPane.showMessageDialog (null, "Too low");
} //=======================
// inherited from BasicGame:
// playManyGames
// playOneGame
// showFinalStatus
}
Did you read my answer and implement it?
I used the Math.abs() method. Works fine, thanks.