-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathScannerExample.java
More file actions
53 lines (41 loc) · 1.98 KB
/
ScannerExample.java
File metadata and controls
53 lines (41 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
/**
* This sample program shows how to use the Scanner class to
* accept input from the user.
*
* The input must match what the program asks for. Be careful to call nextLine()
* after calling nextInt() (for example), to advance to the next line,
* unless of course you expect input after the number.
*/
public class ScannerExample {
public static void main(String[] args) {
/** create a Scanner object to accept input. */
Scanner in = new Scanner(System.in);
/** ask the user for a name and advance to the next line */
System.out.println("Enter your name: ");
String name = in.nextLine();
/** ask the user for an age, then advance to the next line */
System.out.println("Enter your age: ");
int age = in.nextInt();
in.nextLine(); //ignore the rest of the line
/** ask the user for three floats, then advance to the next line */
System.out.println("Enter three numbers: ");
float num1 = in.nextFloat();
float num2 = in.nextFloat();
float num3 = in.nextFloat();
in.nextLine(); //ignore the rest of the line
/** ask the user for a food and advance to the next line */
System.out.println("Enter your favorite food: ");
String food = in.nextLine();
/** ask the user for a number, then treat the rest of the line as the color. Advance to the next line */
System.out.println("Enter your favorite number and color: ");
int favNum = in.nextInt();
String favColor = in.nextLine();
/** output the data */
System.out.println("\n\nHello " + name + "!");
System.out.println("You are " + age + " years old.");
System.out.println("The average of those three numbers is " + ((num1 + num2 + num3)/3));
System.out.println("Your favorite food is " + food + ".");
System.out.println("Your favorite number and color are " + favNum + " and " + favColor + ".");
}
}