Recent Changes - Search:

CS160

Class Wiki

Instructors



edit SideBar

ScannerExample

// ******************************************************************
// ScannerExample.java
// Author: Asa Ben-Hur
// Examples of using expressions, variables and arithmetic operators
// ******************************************************************

import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {

        // instantiate an instance of Scanner which will read from System.in
        Scanner console = new Scanner(System.in);

        // prompt the user for input
        System.out.print("Please type three numbers: ");

        // use scanner to read three integers
        int num1 = console.nextInt();
        int num2 = console.nextInt();
        int num3 = console.nextInt();

        double average = (double) (num1 + num2 + num3) / 3;
        System.out.println("The average is " + average);
    }
}



Edit - History - Print - Recent Changes - Search
Page last modified on September 28, 2008, at 03:30 PM MST