Commit e7b1bda
committed
feat: Add KeyBoard class for summing two user-input numbers
WHAT the code does:
- Defines a `KeyBoard` class with a `main()` method.
- Uses `Scanner` to take two integers (`x` and `y`) from user input.
- Computes their sum `z = x + y`.
- Prints the result in the format: `"Sum is <z>"`.
WHY this matters:
- Demonstrates **reading input from the keyboard** using `Scanner`.
- Reinforces arithmetic operation and formatted output.
- Provides a simple interactive program for practicing user input handling.
HOW it works:
1. Prompts user → `"Enter a two numbers"`.
2. Reads two integers from the console.
3. Adds them and stores in `z`.
4. Prints → `"Sum is <value>"`.
Tips & gotchas:
- `Scanner` should be closed after use (`sc.close()`).
- Input must be valid integers; otherwise, `InputMismatchException` will occur.
- Prompt message `"Enter a two numbers"` could be improved grammatically (e.g., `"Enter two numbers:"`).
- Works for integers only — for floating-point inputs, use `nextDouble()`.
Use-cases:
- Educational example for **basic I/O in Java**.
- Useful for learning how to accept and process user inputs.
- Foundation for building small calculators or interactive CLI programs.
- Prepares for more advanced input-handling tasks.
Short key: class-keyboard-user input-sum.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 60a1905 commit e7b1bda
1 file changed
+2
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
6 | 5 | | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
16 | 14 | | |
17 | 15 | | |
0 commit comments