Skip to content

Commit e7b1bda

Browse files
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

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import java.util.Scanner;
22

33
public class KeyBoard {
4-
public static void main(String args[])
5-
{
4+
public static void main(String args[]) {
65
Scanner sc = new Scanner(System.in);
7-
86
int x,y;
97
System.out.print("Enter a two numbers");
8+
109
x = sc.nextInt();
1110
y = sc.nextInt();
1211

1312
int z = x+y;
1413
System.out.print("Sum is "+z);
15-
1614
}
1715
}

0 commit comments

Comments
 (0)