In the real environment we rarely work with the same values all the time. Prompt the user to provide you the integers, take input from console.
To take input from users we have to use the Scanner class.
Scanner sc= new Scanner(System.in);
int a=sc.nextInt();
int b= sc.nextInt();
from like this we can take input from users. After that we will print a,b as well as their sum. We will use here printf method which belongs in Java PrintStream class. We use it to write a String formatted to output Stream.
It uses the specified format string and arguments.
We can use print/println here, then we don't have to use %4d. We can use then
System.out.printf("Provided integers: " +a +"and "+b+ " the total is "+(a+b));
Note: In the below code I have used Main as the class name as I am doing my code in the online editor, you can name your class according to your code.
Step by step
Solved in 2 steps with 2 images