Thursday, March 31, 2016

Calculating with User Inputs


Hey guys, by this post we're gonna learn how to do calculations with user inputs. 


  1. As usual open the Eclipses
  2. Now import java util package   

Coding :-

  1. First we wanna declare 2 variables.
  2. Next, display message to enter the first number.
  3. Create a scanner to get that number (SN :- When using scanners we get user inputs as Strings)
  4. Initialize that string to first variable.
  5. Do the same to the second number.
  6. Display the answer using System.out.println concept.
     (You also can declare another variable as int total and initialize it as total = fnum + snum; )

import java.util.*;
public class main {
public static void main(String []args){
int fnum;
int snum;
System.out.println("Enter first number");
Scanner s1 = new Scanner(System.in);
fnum = s1.nextInt();
System.out.println("Enter second number");
Scanner s2 = new Scanner(System.in);
snum = s2.nextInt();
System.out.println("Answer is" +fnum+snum);
}
}

Don't try to copy this source code, understand and try this your own )


No comments:

Post a Comment