• Log in or sign up for an account here: https://www.sythe.org/login

Java Tutorial Pt.2 of many! - Building Basic Calculator

diabl0

Apprentice

Banned
Joined
Jul 25, 2008
Sythe Gold
0
Messages
902
I am sure you have read my last thread about "Hello World". If you have not, please check it out.

Today we will be working on creating a little basic calculator.

Okay, as you learned in the last tutorial,
When you open a New class file in Eclipse you will See:

Class Whatever{

}

Well, in this tutorial we will be doing something new, We will be using a scanner.
What the scanner will be used for in java is basically allowing user input.

So we need to import the scanner code. So above your first line of code you should add:

import java.util.scanner;

What this is doing is importing the scanner, so it will allow us to use it in our code.

importt.jpg

Okay, so now that we have got our scanner imported we are good to go!
Remember, under your class line you should always add your main. So lets do that. So now you should have:

class whatever{
public static void main(String args[]){

}
}

mainab.jpg

Okay, let's get started with our coding.

Lets go ahead and add our scanner under our main:
Scanner balls = new Scanner (System.in);

PS. Scanner balls (Balls can be renamed to whatever you want).

So now our code should be:
class whatever{
public static void main(String args[]){
Scanner balls = new Scanner (System.in);

}
}

scannerballs.jpg


Okay so lets go ahead and add a double. So under the scanner code lets add:
double first, second, answer;

What the double means is that unlike an int, which has to be a whole number, with double it allows it to be a fraction or whatever, such as 5.98 or 78.33. whereas int only allows numbers such as 5 or 3 or 6.

The first means first number, second means second number, and of course answer.

doublen.jpg


Okay, so now that is added under the scanner code, lets go ahead and let the user input their first number.

So under the double code, lets add:
System.out.println("Enter your first number: ");

This prints to the screen for the user to enter the first number.
system1f.jpg

So now that it reads that, lets allow the user to input a number. Under that code lets add:

first = balls.nextDouble();

This right here stores the first number entered into your scanner data. Notice the scanner name (Balls) up there.

system2.jpg


Okay so basically the same thing for second number:

system.out.println("Please enter your second number: ");
second = balls.nextDouble();

system3n.jpg


Okay, so now we are almost done. We have got it to print out what we have wanted it to... but how do we get the answer to print out? Easy, First we need to tell the code to add the two together to get answer. so lets add this under the second double line:

answer = first + second;

Thats right, this adds first number and second number to produce answer. This can easily be changed, instead of add, just remove "+" and add one of the following, -, *, /.

Okay so now, how do we get it to print onto the screen? Easy, remember how we told the program to print out what we wrote? Well lets do the same thing except with a twist. You will see, lets add this now for our final line:

System.out.println(answer);

Okay so you noticed what we did there? Instead of having "" inside, and typing in there what we want it to show, we just typed the name of answer so it will show answer. Okay Well that is basically it for a basic calculator, Here should be your final result:

import java.util.scanner;

class whatever{
public static void main(String args[]){
Scanner balls = new Scanner (System.in);
double first, second, answer;
System.out.println("Enter your first number: ");
first = balls.nextDouble();
System.out.println("Enter your second number: ");
second = balls.nextDouble();
answer = first + second;
System.out.println(answer);

}
}

finalq.jpg
 
ty for this I am trying to learn java and I sorta understand know more ty
 
No problem buddy, I am about to add some pictures, so hopefully that will make it easier to learn :)
 
Hey black out your rs name, and thanks for the guide, id rather you just go to the next step of like part 1 like writing more of something than this but this does work also.
 
well that account is banned :P so no need to black it out
 
Runescape Gamblingrunescape gambling
Back
Top