KaiquanMah commited on
Commit
5ce8dad
·
verified ·
1 Parent(s): 0faf456

Scanner reader = new Scanner(System.in);

Browse files
Week 1: Types, condition clauses and loops/6. Print a greeting message ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // use Scanner class
2
+ // to read user input
3
+ import java.util.Scanner;
4
+
5
+ public class Example {
6
+ public static void main(String[] args) {
7
+ System.out.print("Input your name:");
8
+
9
+ Scanner reader = new Scanner(System.in);
10
+ String name = reader.nextLine();
11
+
12
+ System.out.println("Greetings, " + name);
13
+ }
14
+ }
15
+ Example output:
16
+ Input your name: Mike Madeup
17
+ Greetings, Mike Madeup
18
+
19
+
20
+
21
+
22
+
23
+ //eg2
24
+ // int in user input
25
+ //Integer.valueOf
26
+ import java.util.Scanner;
27
+
28
+ public class Example {
29
+ public static void main(String[] args) {
30
+ Scanner reader= new Scanner(System.in);
31
+
32
+
33
+ System.out.print("Give the first number: ");
34
+ int first = Integer.valueOf(reader.nextLine());
35
+
36
+ System.out.print("Give the second number: ");
37
+ int second = Integer.valueOf(reader.nextLine());
38
+
39
+
40
+ int sum = first + second;
41
+ System.out.println("Sum: " + sum);
42
+ }
43
+ }
44
+ Example output:
45
+ Give the first number: 5
46
+ Give the second number: 11
47
+ Sum: 16
48
+
49
+
50
+
51
+
52
+
53
+ //eg3
54
+ //double in user input
55
+ //Double.valueOf
56
+ import java.util.Scanner;
57
+
58
+ public class Example {
59
+ public static void main(String[] args) {
60
+ Scanner reader = new Scanner(System.in);
61
+
62
+ System.out.print("Give the first number: ");
63
+ double first = Double.valueOf(reader.nextLine());
64
+
65
+ System.out.print("Give the second number: ");
66
+ double second = Double.valueOf(reader.nextLine());
67
+
68
+ System.out.print("Give the third number: ");
69
+ double third = Double.valueOf(reader.nextLine());
70
+
71
+ double average= (first + second + third) / 3;
72
+ System.out.println("Average: " + average);
73
+ }
74
+ }
75
+ Example output:
76
+ Give the first number: 2
77
+ Give the second number: 4.5
78
+ Give the third number: 7
79
+ Average: 4.5
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+ """
89
+ Write a program that asks the user to enter their name and then prints a welcome message to the user, as shown in the example below.
90
+
91
+ In the example, the user's input is given in bold. Note that the output must be exactly as shown in the example.
92
+
93
+ Input your name: Ethan Example
94
+ Welcome to the course, Ethan Example!!!
95
+ """
96
+
97
+
98
+
99
+ import java.util.Random;
100
+ import java.util.Scanner;
101
+
102
+
103
+
104
+ public class Test{
105
+ public static void main(String[] args){
106
+ final Random r = new Random();
107
+
108
+ Scanner reader = new Scanner(System.in);
109
+
110
+ System.out.print("Input your name: ");
111
+ String name = reader.nextLine();
112
+
113
+
114
+ System.out.println("Welcome to the course, " + name + "!!!");
115
+
116
+ }
117
+ }
118
+
119
+
120
+
121
+
122
+
123
+ """output
124
+
125
+ Testing with input Ethan Example
126
+ Input your name: Ethan Example
127
+ Welcome to the course, Ethan Example!!!
128
+
129
+ Testing with input Niko Nonexistent
130
+ Input your name: Niko Nonexistent
131
+ Welcome to the course, Niko Nonexistent!!!
132
+
133
+ Testing with input Maddie Madeup
134
+ Input your name: Maddie Madeup
135
+ Welcome to the course, Maddie Madeup!!!
136
+
137
+
138
+
139
+ """