Java - Input Notes
// Must include the import statement at top - import java.util.Scanner;
// Scanner object - Scanner keyboard = new Scanner(System.in);
Scanner keyboard =new Scanner(System.in);
// Input an int
System.out.print("Enter an int: ");
int i = keyboard.nextInt();
System.out.println(i);
// Input a double
System.out.print("Enter a double: ");
double d = keyboard.nextDouble();
System.out.println(d);
// Input a String
System.out.print("Enter a String: ");
String s = keyboard.next();
System.out.println(s);
// NOTE: you should use print instead of println for input prompt
DEMO PROGRAM
// Demo: Have user enter their first name, their favorite number and
// the calculation of their birthYear/birthMonth
Scanner keyboard = new Scanner(System.in);
String name;
double favNum;
double birthYear;
double birthMonth;
double calculation;
System.out.print("What is your name? ");
name = keyboard.next();
System.out.print("What is your favorite number? ");
favNum = keyboard.nextDouble();
System.out.print("What year were you born? ");
birthYear = keyboard.nextDouble();
System.out.print("What is your birth month? (number) ");
birthMonth = keyboard.nextDouble();
calculation = birthYear/birthMonth;
System.out.println("Hello "+name+"! Your favorite num is "+favNum);
System.out.println("Your special number is "+calculation);
Videos |
|
lesson7_part1.swf | |
File Size: | 4968 kb |
File Type: | swf |
lesson7_part2.swf | |
File Size: | 3647 kb |
File Type: | swf |
lesson7_part3.swf | |
File Size: | 2894 kb |
File Type: | swf |
lesson7_part4.swf | |
File Size: | 2634 kb |
File Type: | swf |