Java 2_math

作者: 綿綿_ | 来源:发表于2018-09-02 00:23 被阅读0次
    import java.util.Scanner; // You can import whole libraries of classes like this import java.util.*;
    
    public class LessonTwo
    {
        /* static means that only a class can call for this function to execute
        * Creates a new scanner object named userInput
        * You create the Scanner object by calling new and passing the Scanner constructor
        * the input stream to look at (System.in = keyboard input)
        */ 
        static Scanner userInput = new Scanner(System.in);
        
        public static void main(String[] args)
        {
            System.out.print("Your favorite number: "); // Same as println without a newline
    
                
    // Here I perform basic mathematics calculations
                
                
                numberEntered += 2;   // *=     /=  %=  Also work
                numberEntered -= 2;
                
                // Shorthand way to add 1 to a variable
                numberEntered++;
                
                // Shorthand way to subtract 1 from a variable
                numberEntered--;
                
                int numEnteredABS = Math.abs(numberEntered); // Returns the absolute value
                
                // Returns the larger of the two arguments (They must be of the same type)
                int whichIsBigger = Math.max(5, 7); 
                
                // Returns the smaller of the two arguments (They must be of the same type)
                int whichIsSmaller = Math.min(5, 7);
                
                // Returns the square root argument 
                double numSqrt = Math.sqrt(5.23);
                
                // Rounds the number provided up
                int numCeiling = (int) Math.ceil(5.23);
                System.out.println("Ceiling: " + numCeiling);
                
                // Rounds the number provided down
                int numFloor = (int) Math.floor(5.23);
                System.out.println("Floor: " + numFloor);
                
                // Rounds the number based on the fraction
                int numRound = (int) Math.round(5.23);
                System.out.println("Rounded: " + numRound);
                
                // Generates random numbers between 0 to 9
                int randomNumber = (int) (Math.random() * 10); 
                System.out.println("A random number " + randomNumber);
                
            // If the above condition is false, the code following else is executed 
            } else {
                System.out.println("Sorry you must enter an integer");
            }
            
            
        }
        
    }

    相关文章

      网友评论

        本文标题:Java 2_math

        本文链接:https://www.haomeiwen.com/subject/vdrpwftx.html