Java - Tutorial #2 [Simple Calculator]

Questions/Tutorials for any programming language not covered in other sections.
2 posts Page 1 of 1
Contributors
User avatar
DrNayr
New Member
New Member
Posts: 14
Joined: Thu May 16, 2013 9:06 am

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;
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

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 :)
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
2 posts Page 1 of 1
Return to “Misc”