Page 1 of 1

Java - Tutorial #2 [Simple Calculator]

Posted: Tue May 21, 2013 5:01 pm
by DrNayr
Beginner? to code in java you need an IDE and I prefer you get Eclipse, the best one so far, compile and run your java projects with a user-friendly GUI.


Objective: building a simple calculator

so first of all you need to start Eclipse then start a new Project name it anything you like, then click Next - finish now, right-click on your java project in the explorer, then add new class. for now, name it tutorial.

then open the new class and insert this code:
Code: Select all
import java.util.Scanner;

class apples{
	public static void main(String args[]) {
		Scanner calc = new Scanner(System.in);
		double first, second, total;
		System.out.println("Enter the first number here: ");
		first = calc.nextDouble();
		System.out.println("Enter the second number here: ");
		second = calc.nextDouble();
		total = first + second;
		System.out.println(answer);
	}

}
Explaination:

First we imported Scanner, then we added 3 doubles or integers for this case we use double to be sure decimals work, integers is just like 1 or 6 but double can be 4,3 and 5.8

Then we printed a message to the console saying insert the first number, then we stored it in the "first" double, then we printed a message saying insert second number, and we again stored it to "second" double.

then we just summed it, and then printed the answer!

You can also subtract, multiply and divide using, ( - ) and ( * ) and ( / )

hope it was useful! hehaho;

Re: Java - Tutorial #2 [Simple Calculator]

Posted: Tue May 21, 2013 10:37 pm
by smashapps
It's not required to use an IDE but it does make easier.

Nice tutorial, don't see much Java stuff posted, here's a +rep :)