Results 1 to 10 of 10
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Check if int divides evenly

    What have you tried with the % symbol? It's called the mod operator, by the way.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Check if int divides evenly

    Java 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

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.
    I like likes!

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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?

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.
    I like likes!

  7. #7
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

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

  8. #8
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

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

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

  9. #9
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Check if int divides evenly

    Did you read my answer and implement it?
    I like likes!

  10. #10
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Check if int divides evenly

    I used the Math.abs() method. Works fine, thanks.
    Last edited by Army; 01-08-2013 at 07:09 PM.

Similar Threads

  1. check this out
    By RajeshReddy in forum New To Java
    Replies: 10
    Last Post: 07-25-2011, 12:28 PM
  2. Can someone check this over?
    By Phyxed in forum New To Java
    Replies: 2
    Last Post: 03-29-2011, 09:07 AM
  3. Show all numbers witch divides by 30
    By activex in forum New To Java
    Replies: 2
    Last Post: 10-06-2009, 10:14 AM
  4. Don't know what to check for
    By Chasingxsuns in forum New To Java
    Replies: 8
    Last Post: 08-26-2009, 05:59 AM
  5. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •