Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PowerCalculator

import java.util.Scanner;

/**
 * Performs integer exponentiation with a user-specified base and exponent.
 * 
 * @author Taylor Raker
 * @version September 13, 2017
 */
public class PowerCalculator {
    public static void main(String[ ] args) {
        // ---prompt the user and read input
        System.out.print("Enter two integers (base and exponent):");
        /*^print, not println because it will output the text but the cursor
        will stay on that line and not go to the next one. */
        Scanner in = new Scanner(System.in);
        /*in is a variable of type "Scanner". Constructed a scanner that can 
        read inputs from a keyboard. */
        int base = in.nextInt();
        //variable base of type Int
        int exp = in.nextInt();
        // ----calculate result
        int result = (int) Math.pow(base, exp);
        //static method, "Math". not a class. independent of any object. "pow"
        //performs "power"
        
        
        // ----format and display result
        String output = base + "^" + exp + " is " + result;
        System.out.println(output);
        
    }    
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.