Certainly! Here's a simple Java program that adds two numbers and displays the result:
import java.util.Scanner; public class AddTwoNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first number: "); double firstNumber = input.nextDouble(); System.out.print("Enter the second number: "); double secondNumber = input.nextDouble(); double sum = firstNumber + secondNumber; System.out.println("The sum of " + firstNumber + " and " + secondNumber + " is: " + sum); } }
In this program:
- We import the
java.util.Scanner
class to take input from the user. - We create a
Scanner
object calledinput
to read user input. - We prompt the user to enter two numbers, and we use
input.nextDouble()
to read and store these numbers asfirstNumber
andsecondNumber
. - We calculate the sum of the two numbers and store it in the variable
sum
. - Finally, we print the result, showing the sum of the two numbers.