|
| 1 | +/* |
| 2 | +* Author AngJianming |
| 3 | +* |
| 4 | +*/ |
| 5 | + |
| 6 | +// it is a must to import java.util.Scanner; to access some external files which has the functionality to read input from the console. In Java this is the way to get certain functionalities which is not build in therefore, needing to import them. |
| 7 | + |
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +public class A_05_User_Input { |
| 11 | + public static void main(String[] args) { |
| 12 | + Scanner user_input = new Scanner(System.in); |
| 13 | + System.out.print("Enter your name: "); |
| 14 | + |
| 15 | + String name = user_input.nextLine(); |
| 16 | + System.out.println("My name is " + name); |
| 17 | + |
| 18 | + |
| 19 | + // Example Calculator |
| 20 | + Scanner scanner = new Scanner(System.in); |
| 21 | + |
| 22 | + // Prompt the user to enter the first number |
| 23 | + System.out.print("Enter the first integer: "); |
| 24 | + int x = scanner.nextInt(); |
| 25 | + |
| 26 | + // Prompt the user to enter the second number |
| 27 | + System.out.print("Enter the second integer: "); |
| 28 | + int y = scanner.nextInt(); |
| 29 | + |
| 30 | + // Perform arithmetic operations |
| 31 | + int sum = x + y; // Addition |
| 32 | + int difference = x - y; // Subtraction |
| 33 | + int product = x * y; // Multiplication |
| 34 | + |
| 35 | + // To handle division by zero, you can check if b == 0 |
| 36 | + // but for simplicity, we assume b != 0 in this example |
| 37 | + double quotient = (double) x / y; // Division |
| 38 | + int modulus = x % y; // Modulus (remainder) |
| 39 | + |
| 40 | + // Print the results |
| 41 | + System.out.println("\n--- Results ---"); |
| 42 | + System.out.println(x + " + " + y + " = " + sum); |
| 43 | + System.out.println(x + " - " + y + " = " + difference); |
| 44 | + System.out.println(x + " ×ばつ " + y + " = " + product); |
| 45 | + System.out.println(x + " ÷ " + y + " = " + quotient); |
| 46 | + System.out.println(x + " % " + y + " = " +"R"+ modulus); |
| 47 | + |
| 48 | + // Close the scanner |
| 49 | + scanner.close(); |
| 50 | + } |
| 51 | +} |
0 commit comments