Adding Integers in a Command Window
This program adds two integers using scanner a java class as input
(//) is used as a comment to explain the program more clearly
import java.util.Scanner; // this imports class Scanner in java.utility
//A Scanner enables a program to read data. data which are numbers and strings for use in a program
//Note: Always leave white line spaces in between your programs to be more readable and clear.
public class AddingIntegersUsingScanner {
//the public class begins a class declaration for class AddingIntegersUsingScanner. Every program in java consists of at least
//one class declaration that is defined by you the programmer.
public static void main (String [] args) // the starting point of every java applications
{
//create a scanner to obtain input from the command prompt
Scanner input = new Scanner(System.in);
int number1; // first number to add
int number2; //second number to add
int sum; // sum of number number 1 and number 2
System.out.print("Enter first integer: "); // prompts user to input first integer
number1 = input.nextInt(); // reads in integer from the user
System.out.print("Enter second integer: "); // prompts user to input second integer
number2 = input.nextInt(); // prompts user to input second integer
sum = number1 + number2; //adds the integers
System.out.printf("Sum is %d\n", sum) //displays the results
}
}
0 comments :
Post a Comment