# Check if int divides evenly

• 01-07-2013, 06:58 PM
Army
Check if int divides evenly
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.
• 01-07-2013, 07:00 PM
KevinWorkman
Re: Check if int divides evenly
What have you tried with the % symbol? It's called the mod operator, by the way.
• 01-07-2013, 07:35 PM
Army
Re: Check if int divides evenly
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
• 01-07-2013, 07:46 PM
Sierra
Re: Check if int divides evenly
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.
• 01-08-2013, 12:18 AM
Army
Re: Check if int divides evenly
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?
• 01-08-2013, 12:43 AM
Sierra
Re: Check if int divides evenly
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.
• 01-08-2013, 03:59 AM
foulkelore
Re: Check if int divides evenly
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.
• 01-08-2013, 05:31 PM
Army
Re: Check if int divides evenly
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.

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 }```
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.
• 01-08-2013, 06:20 PM
Sierra
Re: Check if int divides evenly